【问题标题】:Sinatra ActiveRecord Model ProblemsSinatra ActiveRecord 模型问题
【发布时间】:2014-07-12 15:01:30
【问题描述】:

所以我正在编写我的第一个 Sinatra 应用程序,并使用 ActiveRecord 作为 ORM。

所以在我的 app.rb 中,我有一个

class User < ActiveRecord::Base
    validates_uniqueness_of :username
    validates_presence_of :username
end

并创建一个迁移并在此运行 rake db:migrate

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :username
      t.string :email
      t.string :password
      t.string :name
      t.float :rating
      t.timestamps
    end
  end
 end

但是,当我在我的一种方法中运行时

post "/create" do
    u = User.new
    u.save
    redirect '/'
end

Sinatra 在User.new 上崩溃并说 没有获取数据。在上面。我看过教程,不知道。

【问题讨论】:

    标签: ruby activerecord sinatra sinatra-activerecord


    【解决方案1】:

    我想通了。

    我在函数之前声明了 User 类,所以它看不到该类。

    【讨论】:

      猜你喜欢
      • 2015-06-23
      • 1970-01-01
      • 1970-01-01
      • 2014-07-10
      • 2013-05-17
      • 2013-07-31
      • 1970-01-01
      • 2013-07-04
      • 1970-01-01
      相关资源
      最近更新 更多