I'm trying to get Bundler setup so I can deploy my Sinatra app to server with all the correct gems.
I've created my Gemfile
source :gemcutter
gem 'sinatra', '1.0'
gem "nokogiri", "1.4.2"
gem "rack", "1.1.0"
gem "dm-core", "1.0.0"
gem "dm-migrations", "1.0.0"
gem "dm-sqlite-adapter", "1.0.0"
gem "pony", "1.0"
Next I created a Config.ru
require 'rubygems'
require 'bundler'
Bundler.setup
require 'sinatra'
require 'dm-core'
require 'dm-migrations'
require 'dm-sqlite-adapter'
require 'open-uri'
require 'nokogiri'
require 'csv'
require 'pony'
require 'parsedate'
require 'digest/md5'
require 'MyApp'
run MyApp
So far so good, so next I ran bundle install and got 'Bundle Complete' so now all I need to do is just Rackup
Then I get:
config.ru:18: undefined local variable or method `MyApp' for #<Rack::Builder:0x1227350 #ins=[]> (NameError)
from /usr/local/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/builder.rb:46:in `instance_eval'
from /usr/local/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/builder.rb:46:in `initialize'
from config.ru:1:in `new'
from config.ru:1
Here's a simple MyApp.rb which will trigger the same error
get '/' do
erb :index
end
What's going wrong? :(
If you tell Rack to run MyApp, you need to define class MyApp first (which you're supposed to do inside MyApp.rb). Derive your class from Sinatra::Base to make it a Sinatra-Rack-App that can be run from config.ru:
require 'sinatra/base'
class MyApp < Sinatra::Base
get '/' do
erb :index
end
end
See also Sinatra's README about modular Sinatra apps (search for the paragraph named "Modular Apps" on http://github.com/sinatra/sinatra/)
Additionally you may have your my_app.rb as follows:
require 'rubygems'
require 'bundler'
Bundler.setup
require 'sinatra'
require 'dm-core'
require 'dm-migrations'
require 'dm-sqlite-adapter'
require 'open-uri'
require 'nokogiri'
require 'csv'
require 'pony'
require 'parsedate'
require 'digest/md5'
And your config.ru like this:
require './my_app'
run Rack::URLMap.new '/' => Sinatra::Application
Hope this helps.
Best Regards
ED
As an alternative to creating a modular app (wrapping your Sinatra methods in a class extending Sinatra::Base), you can use:
run Sinatra::Application
in the config.ru file in place of
run MyApp
This might be a better option if you want to keep the simple Sinatra code.
See the docs for more info.
Related
What am I missing? I've included slim in my Gemfile & 'required' it.
config.ru:
require './sinatra.rb'
run Sinatra::Application
Gemfile:
source 'https://rubygems.org'
gem 'rack'
gem 'sinatra'
gem 'slim'
sinatra.rb:
require 'sinatra'
require 'slim'
get '/' do
slim :index
end
# Gemfile:
source 'https://rubygems.org'
ruby '1.9.3'
gem 'sinatra'
gem 'figaro'
gem 'octokit'
# app.rb
require 'sinatra'
require 'json'
require 'cgi'
require 'octokit'
require 'figaro'
class Application < Sinatra::Base
get '/' do
'Hi'
end
end
# config.ru
require './app'
$stdout.sync = true
run Application
When push:
-----> Ruby/Rails app detected
However, if I remove figaro gem and repush it works fine.
You answered it yourself:
figaro depend on Rails (see the gemspec: https://github.com/laserlemon/figaro/blob/master/figaro.gemspec). Heroku detect rails application by looking if their Gemfile.lock contains the Railties gem, which is a dependency of rails.
So: yourapp -> figaro -> rails -> railties.
Hence the identification of your gem as a Rails App. Why are you using Figaro if not to help configure a Rails application anyway?
I'm trying to use httparty
require 'httparty'
but I'm getting
no such file to load -- httparty (LoadError)
I'm using Ruby 1.8.7 on OSX
On Ruby 1.8, you have to do require "rubygems" before you can require any Gems in your code. So:
require "rubygems"
require "httparty"
# Your code here.
hi, all
I build a sinatra app, the main files for bundling as the following,
environment.rb
require 'sinatra'
require 'sequel'
ENV['RACK_ENV'] = 'development'
configure :production do
#do something
end
configure :development, :test do
#do something
end
Gemfile
gem 'sinatra'
gem 'sequel'
gem 'pg', :group => :production
gem 'sqlite3', :group => [:development, :test]
So, how to let the bundle install based on the ENV['RACK_ENV'] in my environment.rb file.
When doing a bundler require you can specify which groups to be required.
For example:
require 'rubygems'
require 'bundler'
if ENV['RACK_ENV'] == 'development'
Bundler.require(:default, :development)
else
Bundler.require(:default)
require 'sinatra'
More info on the bundler site gemfile specifications found here.
Gemfile
source :rubygems
gem 'sinatra'
config.ru
require 'app'
run App
app.rb
require 'bundler/setup'
require 'sinatra'
class App < Sinatra::Base
get '/' do
'hello world'
end
end
rackup failes with
.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': no such file to load -- app (LoadError)
Works with ruby 1.8 . Why?
I think it's because 1.9.2 no longer includes '.' in the load path by default.
See the this question for more: Why does Ruby 1.9.2 remove "." from LOAD_PATH, and what's the alternative?
Some notes:
Gemfile, I use gem 'sinatra', :require => 'sinatra/base' to load a Modular Sinatra App.
Config.ru, usually I set Bundler on it, not in app.rb, leaving app.rb clean to my app.
require 'bundler/setup'
Bundler.require(:default)