【问题标题】:Sorcery gem with has_one association具有 has_one 关联的魔法宝石
【发布时间】:2012-03-25 07:13:18
【问题描述】:

现在我正在使用此代码:

用户 has_one User_extra

用户 => :username, :email, :crypted_pa​​ssword, :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

可能是什么问题??

【问题讨论】:

  • 您将attr_accessible :gender, :address, ...添加到哪个型号?

标签: ruby-on-rails ruby sorcery


【解决方案1】:

您应该将 attr_accessible :gender、:address 等添加到 UserExtra 模型中

【讨论】:

  • 啊,我想试试这个,但没有成功
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多