Sinatra错误:NoMethodError:未定义的方法`get'for#<class:0x007fa70444aef0>

时间:2015-05-17 02:25:03

标签: activerecord sqlite sinatra

我正在使用Sinatra和ActiveRecord创建一个网站。每当我运行我的网站时,我都会收到错误

Boot Error

Something went wrong while loading config.ru

NoMethodError: undefined method `get' for #<Class:0x007f8b646d8818>

/Users/keturahwalters/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.2/lib/active_record/dynamic_matchers.rb:22:in `method_missing'
/Users/keturahwalters/Desktop/Projects/char_site/app.rb:20:in `<class:Contact>'
/Users/keturahwalters/Desktop/Projects/char_site/app.rb:9:in `<top (required)>'
config.ru:1:in `require'
config.ru:1:in `block in inner_app'
/Users/keturahwalters/.rvm/gems/ruby-2.0.0-p0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
/Users/keturahwalters/.rvm/gems/ruby-2.0.0-p0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
config.ru:1:in `new'
config.ru:1:in `inner_app'
/Users/keturahwalters/.rvm/gems/ruby-2.0.0-p0/gems/shotgun-0.9/lib/shotgun/loader.rb:112:in `eval'
/Users/keturahwalters/.rvm/gems/ruby-2.0.0-p0/gems/shotgun-0.9/lib/shotgun/loader.rb:112:in `inner_app'
/Users/keturahwalters/.rvm/gems/ruby-2.0.0-p0/gems/shotgun-0.9/lib/shotgun/loader.rb:102:in `assemble_app'
/Users/keturahwalters/.rvm/gems/ruby-2.0.0-p0/gems/shotgun-0.9/lib/shotgun/loader.rb:86:in `proceed_as_child'
/Users/keturahwalters/.rvm/gems/ruby-2.0.0-p0/gems/shotgun-0.9/lib/shotgun/loader.rb:31:in `call!'
/Users/keturahwalters/.rvm/gems/ruby-2.0.0-p0/gems/shotgun-0.9/lib/shotgun/loader.rb:18:in `call'
/Users/keturahwalters/.rvm/gems/ruby-2.0.0-p0/gems/shotgun-0.9/lib/shotgun/favicon.rb:12:in `call'
/Users/keturahwalters/.rvm/gems/ruby-2.0.0-p0/gems/shotgun-0.9/lib/shotgun/static.rb:14:in `call'
/Users/keturahwalters/.rvm/gems/ruby-2.0.0-p0/gems/rack-1.5.2/lib/rack/builder.rb:138:in `call'
/Users/keturahwalters/.rvm/gems/ruby-2.0.0-p0/gems/rack-1.5.2/lib/rack/handler/webrick.rb:60:in `service'
/Users/keturahwalters/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/Users/keturahwalters/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/Users/keturahwalters/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

这是我的app.rb

require 'bundler'
Bundler.require

require "sinatra"
require "sinatra/activerecord"

set :database, "sqlite3:///events.db"

class Contact < ActiveRecord::Base

    validates_uniqueness_of :first
    validates_presence_of :first

    validates_uniqueness_of :last
    validates_presence_of :last

    validates_uniqueness_of :email
    validates_presence_of :email

    get '/' do
        erb :index 
    end

    get '/weddings.erb' do
        erb :weddings
    end

    get '/birthdays.erb' do
        erb :birthdays
    end

    get '/anniversaries.erb' do
        erb :anniversaries
    end

    get '/about.erb' do
        erb :about
    end

    get '/contacts/new' do
        @contact = Contact.new
        erb :contact
    end

    post '/contacts' do
        @contact = Contact.new(params[:rabbit])
        if @contact.save
            status 201
            redirect '/' + @contact.id.to_s
        else
            status 400
            erb :contact
        end
    end

end

这是我的config.ru

require "./app"
run Sinatra::Application

任何人都知道问题是什么?

1 个答案:

答案 0 :(得分:1)

您已经从ActiveRecord::Base继承了数据库模型。将您的路由放在继承自Sinatra::Base的类中,并确保require 'sinatra/base'