0

我对嵌套属性的表单感到困惑

这是我的模型:

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

关于哪里出错的任何建议?

4

1 回答 1

1

它应该只是f.fields_for :user_email_preference do |email|。这将设置正确的嵌套属性名称。

于 2014-03-17T07:50:34.937 回答