私にはユーザーがいます。これは、管理者、学生、教師の3つのタイプのいずれかになります。誰もが他の属性を持っています。私はこのように1対1でポリモーフィックな関連付けを試みています:
ユーザー
class User < ActiveRecord::Base
belongs_to :identity, :polymorphic => true
accepts_nested_attributes_for :identity, :allow_destroy => true
attr_accessible :email, :login, :remember_token,
:password_confirmation, :password, :role
end
学生
class Student < ActiveRecord::Base
attr_accessible :field
has_one :user, :as => :identity
end
コントローラ
def new
@user = User.new
end
def create
@user = User.new(params[:user]) # It fails here.
@user.identita.build
...
end
意見
<%= form_for(@user) do |f| %>
<%= f.label :login %><br />
<%= f.text_field :login %>
<%= f.fields_for [:identity, Student.new] do |i| %>
<%= i.label :field %><br />
<%= i.textfield_select :field %>
<% end %>
<% end %>
このビューを送信すると(より複雑ですが、これがコアです)、次のようなハッシュが送信されます。
{"utf8"=>"✓",
"authenticity_token"=>"...",
"user"=>{"login"=>"...",
"student"=> {"field"=>"..."}
}
したがって、コントローラーのマークされた行で次のように失敗します。
ActiveModel::MassAssignmentSecurity::Error in UsersController#create
Can't mass-assign protected attributes: student
私は何が間違っているのですか?:as => "student"のようなもの、または実現をねじる?