Rails 3为具有嵌套属性的模型构成帮助器

时间:2014-03-17 07:48:39

标签: ruby-on-rails-3 forms nested-attributes attr-accessible

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

这是我的模特:

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

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

1 个答案:

答案 0 :(得分:1)

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