Rails:红宝石在导轨上的未知属性错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Rails:红宝石在导轨上的未知属性错误相关的知识,希望对你有一定的参考价值。

我正在浏览railstutorial.org书,并且在代码中遇到未知属性错误

我的User.rb文件代码是

# == Schema Information
#
# Table name: users
#
#  id         :integer         not null, primary key
#  name       :string(255)
#  email      :string(255)
#  created_at :datetime        not null
#  updated_at :datetime        not null
#

class User < ActiveRecord::Base
  attr_accessible :email, :name, :password, :password_confirmation

 validates :name,presence:true,length: { maximum:50 }
VALID_EMAIL_REGEX = /A[w+-.]+@[a-zd-.]+.[a-z]+z/i

validates :email,presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness:{ :case_sensitive => false }
before_save { |user| user.email = email.downcase }



end

和我的user_spec.rb文件代码是

require 'spec_helper'
describe User do
before do
@user = User.new(name: "Example User", email: "user@example.com" , password:"foobar", password_confirmation: "foobar")
end
subject { @user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
it {should respond_to(:password) }
it {should respond_to(:password_confirmation)}

it { should be_valid }
describe "when name is not present" do
before { @user.name = " " }
it { should_not be_valid }
end
describe "when name is too long" do
before { @user.name = "a" * 51 }
it { should_not be_valid }
end
describe "when email format is invalid" do
it "should be invalid" do
addresses = %w[user@foo,com user_at_foo.org example.user@foo.
foo@bar_baz.com foo@bar+baz.com]
addresses.each do |invalid_address|
@user.email = invalid_address
@user.should_not be_valid
end
end
end
describe "when email format is valid" do
it "should be valid" do
addresses = %w[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
addresses.each do |valid_address|
@user.email = valid_address
@user.should be_valid
end
end
end
describe "when email address is already taken" do
before do
user_with_same_email = @user.dup
user_with_same_email.save
end
it { should_not be_valid }
end
describe "when email address is already taken" do
before do
user_with_same_email = @user.dup
user_with_same_email.email = @user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
describe "when password is not present" do
before { @user.password = @user.password_confirmation = " " }
it { should_not be_valid }
end
describe "when password doesn't match confirmation" do
before { @user.password_confirmation = "mismatch" }
it { should_not be_valid }
end

end

终端中发生的事情是

ritesh@ritesh-desktop:~/projects/sample_app$ rails console
Loading development environment (Rails 3.2.9)
1.9.3p327 :001 > User.create(name: "Michael Hartl", email: "mhartl@example.com", password: "foobar", password_confirmation: nil)
ActiveRecord::UnknownAttributeError: unknown attribute: password
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/attribute_assignment.rb:88:in `block in assign_attributes'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/attribute_assignment.rb:78:in `each'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/attribute_assignment.rb:78:in `assign_attributes'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/base.rb:497:in `initialize'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/persistence.rb:44:in `new'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/persistence.rb:44:in `create'
    from (irb):1
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
    from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.9/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
1.9.3p327 :002 > 

请提供一些线索以解决该错误!

答案

通过生成迁移来添加列:

rails generate migration AddPasswordToUsers password:string

然后运行:

rake db:migrate

但是,虽然您尚未将其添加到用户模型中,但似乎正在尝试使用has_secure_password。如果是这种情况,请将行has_secure_password添加到您的用户模型中,然后运行此迁移:

rails generate migration AddPasswordDigestToUsers password_digest:string
另一答案

[在生产环境中使用PostgreSQL数据库的Ruby on Rails应用程序时,我遇到了同样的问题。

这是我的解决方法

问题是我使用迁移文件在开发环境的表中添加了一些新列,但是当我推送到生产环境时,我没有创建这些列通过迁移创建新列。

我要做的只是在生产环境中运行数据库迁移。

rails db:migrate

就这些。

我希望这会有所帮助

以上是关于Rails:红宝石在导轨上的未知属性错误的主要内容,如果未能解决你的问题,请参考以下文章

红宝石 紫外线、导轨、豹纹

hashie / extensions / dash / indifferent_access(LoadError)导轨

响应多个 JSON 渲染。 (红宝石/导轨)

ruby on rails 在制作新应用程序时显示未知编码名称。在 Windows 7 上,导轨 4.2

从滑轨3.2升级到导轨4

(sass):26096 如何修复 Rails 中的“Sass::SyntaxError: Invalid CSS after”错误