Model attribute is not being set when using devise?
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 => 'myemail@gmail.com', :password => 'apassword')
@user.valid?
puts "user's first n开发者_开发百科ame: #{@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?
In your User
model, write this:
attr_accessible :first_name, :last_name
and you are good to go.
精彩评论