views:

280

answers:

1

Hi,

I'm trying to get Bundler setup so I can deploy my Sintra a 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? :(

+1  A: 

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/)

Andreas
Thanks, seems to be working ok now. I'd never come across Sinatra::Base before.Just to note that I updated my Config.ru to read "MyApp.run! :host => 'localhost', :port => 9090" didn't realised I also had to bind a port in there to get it started up.
Tom
Using MyApp.run! inside config.ru is probably not, what you want. MyApp.run! starts a new server process that serves your app (therefore you need to tell it the port). Inside config.ru, you want to tell the already running webserver (the one that just started up and is parsing config.ru, e.g. unicorn) how to direct http requests to your app (by telling it `run MyApp` inside config.ru). MyApp.run! starts up a completely different standalone server that (afaik) does not use config.ru, it's meant for standalone usage (e.g. during development) and should not be used inside config.ru.
Andreas
Thanks, yep MyApp.Run! is totally not what I wanted at all.
Tom