使用设备时未设置模型属性?
我使用 devise 来创建用户模型。它包含 devise 中的所有默认内容以及另外 2 个数据库列:first_name 和 last_name。很简单的东西。
我正在编写一些测试来尝试一下:
@user = User.new(:first_name => "Ken", :email => '[email protected]', :password => 'apassword')
@user.valid?
puts "user's first name: #{@user.first_name}"
puts @user.errors
在我的模型中,我有这个:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me
validates_presence_of :first_name
end
出于某种原因,当我在构造函数中传递“first_name”时,它没有被设置。密码和电子邮件已设置,但名字尚未设置。
有谁知道为什么?我玩过一些 Rails,它也适用于其他模型。为什么不使用设计呢?
I used devise to create a user model. It contains all the default stuff from devise as well as 2 more database columns: first_name and last_name. Pretty simple stuff.
I am coding some tests to try it out:
@user = User.new(:first_name => "Ken", :email => '[email protected]', :password => 'apassword')
@user.valid?
puts "user's first name: #{@user.first_name}"
puts @user.errors
In my model, I have this:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me
validates_presence_of :first_name
end
For some reason, "first_name" is not getting set when I pass it in the constructor. The password and email are getting set, but not first_name.
Does anyone know why? I have played a bit with rails, and it works in other models. Why not with devise?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的
User
模型中,写下以下内容:然后您就可以开始了。
In your
User
model, write this:and you are good to go.