0

今私はこのコードを使用しています:

ユーザーhas_oneUser_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に1つ:

attr_accessible :gender, :address ...

しかし、とにかくエラーをスローしています:

Can't mass-assign protected attributes: date_birth(1i), date_birth(2i), date_birth(3i), gender, address

何が問題なのか?

4

1 に答える 1

1

attr_accessible:gender、:addressなどをUserExtraモデルに追加する必要があります

于 2012-03-24T23:13:18.640 に答える