Search code examples
rubysinatrayamlconfiguration-files

sinatra config.yml settings per environment don't work


I tried to set up a config file for my sinatra app, and simple settings work. (the puts in my code prints "Hello!!") But I read this document, which says I can define settings per environment. I tried it but it doesn't work. What am I doing wrong? ;) I'm using this code:

#settings.yml
#environments: :production, :development
environment: :development

bla: Hello!!

development:
  db_adapter:   "sqlite3"
  db_location:  "db/development"
  db_logfile:   $stdout
  db_loglevel:  :debug
  haml: "format => :html5, :ugly => true"

production:
  db_adapter:   "sqlite3"
  db_location:  "db/production"
  db_logfile:   "log/production_db.log"
  db_loglevel:  :error

and

#main.rb
#!/usr/bin/ruby

require 'rubygems'
require 'sinatra'             #Webframework
require 'sinatra/config_file' #Config

set :environment, :development
config_file("settings.yml")

puts settings.bla

DataMapper::Logger.new(settings.db_logfile, settings.db_loglevel)
DataMapper.setup(:default, "#{settings.db_adapter}://#{Dir.pwd}/#{settings.db_location}")
...

It raises a noMethodError for settings.db_logfile... sorry for my bad english ;)

EDIT1: I got it ;) I have to write this into my main.rb:

set :environment, :development

configure :development do
  set :db_adapter     ,  "sqlite3"
  set :db_location    ,  "db/development"
  set :db_logfile     ,  $stdout
  set :db_loglevel    ,  :debug
  set :haml           ,  :format => :html5
end

Solution

  • You want to configure your environments like this:

    configure :production do
      set :db_adapter, "sqlite3"
      set :db_location, "db/production"
      set :db_logfile, "log/production_db.log"
      set :db_loglevel, :error
    end
    
    configure :development do
      set :db_adapter, "sqlite3"
      set :db_location, "db/development"
      set :db_logfile, $stdout
      set :db_loglevel, :debug
      set :haml, {:format => :html5, :ugly => true}
    end
    
    configure :test do
      ...
    end
    

    see Sinatra Readme