我有用户,它可以是三种类型之一:管理员、学生、教师。每个人都有其他属性。我正在尝试像这样一对一的多态关联:
用户
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" 或扭曲现实?