我有一个简单的 sinatra-activerecord 应用程序。在文件 ./config/environments.rb 我有以下配置:
configure :development, :test do
set :database, 'sqlite://database.db'
end
# These Heroku setup instructions can be at: https://devcenter.heroku.com/articles/rack
configure :production do
# Database connection
db = URI.parse(ENV['DATABASE_URL'] || 'postgres://localhost/mydb')
ActiveRecord::Base.establish_connection(
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
:host => db.host,
:username => db.user,
:password => db.password,
:database => db.path[1..-1],
:encoding => 'utf8'
)
end
当我推送到 Heroku 时,数据库设置工作得很好。但是,在本地,它没有。例如,如果我这样做
$ rake db:migrate
我得到以下良好的输出:
== CreateHotels: migrating ===================================================
-- create_table(:hotels)
-> 0.0015s
-- add_index(:hotels, :hotel_name)
-> 0.0004s
== CreateHotels: migrated (0.0021s) ==========================================
现在,如果我启动服务器并使用 curl 拨打电话:
$ shotgun app.rb
$ curl -d "hotel_name=Sheraton" http://127.0.0.1:9393/hotels/
我收到以下错误:
ActiveRecord::StatementInvalid - Could not find table 'hotels':
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.8/lib/active_record/connection_adapters/sqlite_adapter.rb:472:in `table_structure'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.8/lib/active_record/connection_adapters/sqlite_adapter.rb:346:in `columns'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.8/lib/active_record/connection_adapters/schema_cache.rb:12:in `block in initialize'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.8/lib/active_record/model_schema.rb:228:in `yield'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.8/lib/active_record/model_schema.rb:228:in `default'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.8/lib/active_record/model_schema.rb:228:in `columns'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.8/lib/active_record/model_schema.rb:243:in `column_defaults'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.8/lib/active_record/base.rb:482:in `initialize'
/Users/Sy/Developer/Sinatra/stay-checkin-api/routes/hotels_routes.rb:8:in `new'
/Users/Sy/Developer/Sinatra/stay-checkin-api/routes/hotels_routes.rb:8:in `block in <top (required)>'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:1265:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:1265:in `block in compile!'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:835:in `[]'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:835:in `block (3 levels) in route!'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:851:in `route_eval'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:835:in `block (2 levels) in route!'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:872:in `block in process_route'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:870:in `catch'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:870:in `process_route'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:834:in `block in route!'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:833:in `each'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:833:in `route!'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:936:in `dispatch!'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:769:in `block in call!'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `block in invoke'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `catch'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `invoke'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:769:in `call!'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:755:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/rack-protection-1.2.0/lib/rack/protection/xss_header.rb:22:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/rack-protection-1.2.0/lib/rack/protection/path_traversal.rb:16:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/rack-protection-1.2.0/lib/rack/protection/json_csrf.rb:17:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/rack-protection-1.2.0/lib/rack/protection/base.rb:47:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/rack-protection-1.2.0/lib/rack/protection/xss_header.rb:22:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/nulllogger.rb:9:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/head.rb:9:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/methodoverride.rb:21:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/showexceptions.rb:21:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:99:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:1389:in `block in call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:1471:in `synchronize'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:1389:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/lint.rb:48:in `_call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/lint.rb:36:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/showexceptions.rb:24:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/commonlogger.rb:20:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:136:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/shotgun-0.9/lib/shotgun/loader.rb:86:in `proceed_as_child'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/shotgun-0.9/lib/shotgun/loader.rb:31:in `call!'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/shotgun-0.9/lib/shotgun/loader.rb:18:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/shotgun-0.9/lib/shotgun/favicon.rb:12:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/builder.rb:134:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/thin-1.4.1/lib/thin/connection.rb:80:in `block in pre_process'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/thin-1.4.1/lib/thin/connection.rb:78:in `catch'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/thin-1.4.1/lib/thin/connection.rb:78:in `pre_process'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/thin-1.4.1/lib/thin/connection.rb:53:in `process'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/thin-1.4.1/lib/thin/connection.rb:38:in `receive_data'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run_machine'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/thin-1.4.1/lib/thin/backends/base.rb:63:in `start'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/thin-1.4.1/lib/thin/server.rb:159:in `start'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/handler/thin.rb:13:in `run'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/shotgun-0.9/bin/shotgun:156:in `<top (required)>'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/bin/shotgun:19:in `load'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/bin/shotgun:19:in `<main>'
任何的想法?
ps:这是我的app.rb、config.rb、路由和模型文件。
应用程序.rb
require 'sinatra'
require 'sinatra/activerecord'
require 'uri'
require './config/environments'
require_relative 'models/init'
require_relative 'routes/init'
配置文件
require './app'
run Sinatra::Application
型号:hotel.rb
class Hotel < ActiveRecord::Base
validates :hotel_name, :presence => true
end
路线:hotels_routes.rb
get '/hotels' do
@hotels = Hotel.all
if @hotels
content_type :json
@hotels.to_json
end
end
post '/hotels/' do
@hotel = Hotel.new
@hotel.hotel_name = params[:hotel_name]
if @hotel.save
content_type :json
@hotel.to_json
else
content_type :json
"Failed to create new hotel with name #{params[:hotel_name]}".to_json
end
end
宝石文件
source :rubygems
gem 'heroku'
gem 'sinatra'
gem 'thin'
gem 'activerecord'
gem 'sinatra-activerecord'
gem 'json'
# Define which database gem to use depending on whether you are in local or production environment
group :development, :test do
gem 'sqlite3'
end
group :production do
gem 'pg' # this gem is required to use postgres on Heroku
end
耙文件
require './app'
require 'sinatra/activerecord/rake'