现在我正在使用此代码:
用户 has_one User_extra
用户 => :username,:email,:crypted_password,:salt,:mobile
User_extra => :user_id,:date_birth,:gender,:address
user.rb
class User < ActiveRecord::Base
authenticates_with_sorcery!
attr_accessible :username, :email, :password, :password_confirmation, :first_name,
:user_extra_attributes
has_one :user_extra, :dependent => :destroy
accepts_nested_attributes_for :user_extra
end
user_extra.rb
class UserExtra < ActiveRecord::Base
belongs_to :user
end
users_controller.rb
def new
@user = User.new
@user.build_user_extra
end
def edit
@user = User.find_by_permalink(params[:id])
@user.build_user_extra
end
如果你使用了Sorcery gem,你可能知道应该将任何新属性添加到 attr_accessible ,所以在我的情况下它是: user_extra_attributes ,但如果我添加它,然后出现错误:
Can't mass-assign protected attributes: date_birth(1i), date_birth(2i), date_birth(3i),
gender, address
,然后我将它们逐个添加到attr_accessible,如下所示:
attr_accessible :gender, :address ...
但无论如何都会抛出错误:
Can't mass-assign protected attributes: date_birth(1i), date_birth(2i), date_birth(3i), gender, address
可能是什么问题?
答案 0 :(得分:1)
您应该将attr_accessible:gender,:address等添加到UserExtra模型