我对嵌套属性的形式感到困惑
这是我的模型:
class User < ActiveRecord::Base
has_one :user_email_preference, :dependent => :destroy
accepts_nested_attributes_for :user_email_preference
attr_accessible :user_email_preference_attributes
这是我对设计的看法:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
<%= f.fields_for resource.user_email_preference do |email| %>
<li>
<h3><%= email.label :alerts %></h3>
<div><%= email.check_box :alerts, class:"js-switch" %></div>
</li>
<li>
<h3><%= email.label :newsletter %></h3>
<div><%= email.check_box :newsletter, class:"js-switch" %></div>
</li>
<% end %>
<% end %>
使用http get正确呈现视图。但是,不断提交表单会给我以下错误消息:
Can't mass-assign protected attributes: user_email_preference
有什么建议吗?
应该是f.fields_for :user_email_preference do |email|
。这会设置正确的嵌套属性名称。