开发者

Sinatra and Bundler

开发者 https://www.devze.com 2023-01-10 11:07 出处:网络
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

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::Bu开发者_StackOverflow社区ilder: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.

0

精彩评论

暂无评论...
验证码 换一张
取 消