2012-09-19 58 views
0

我有一個簡單的sinatra-activerecord應用程序。 ./config/environments.rb我有以下的配置文件裏面:未找到Sinatra-activerecord表錯誤

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>' 

A你有想法嗎?

ps:這裏是我的app.rb,config.rb,routes和model文件。

app.rb

require 'sinatra' 
require 'sinatra/activerecord' 
require 'uri' 
require './config/environments' 


require_relative 'models/init' 
require_relative 'routes/init' 

config.rb

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 

的Gemfile

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 

Rake文件

require './app' 
require 'sinatra/activerecord/rake' 
+0

有幾個問題:1)你確認數據庫設置在Heroku上工作正常嗎,還是你假設因爲它給了你沒有錯誤? 2)你是否通過命令行客戶端檢查了sqlite數據庫?我現在也會建議你安裝postgres並對其進行開發,這並不難,而且從長遠來看,如果不是短期的話,也會節省你的麻煩。 https://devcenter.heroku.com/articles/local-postgresql – iain

+0

是的,我做了幾個API調用heroku並得到有效的JSON響應。不太信任,我沒有檢查日誌,它很好。我想我會遵循你的建議,並在本地開發針對postgres。在問這個問題之前,我做了大量的研究,並且確實看到一些人回來幾個月說本地的sinatra + sqlite不值得這樣做。感謝您的鏈接! – sybohy

回答

1

數據庫中設置是錯誤的,請嘗試:

set :database, 'sqlite:///database.db'