Rails-质量分配错误的Accepts_nested_attributes_ [英] Rails - Accepts_nested_attributes_for mass assignment error

查看:66
本文介绍了Rails-质量分配错误的Accepts_nested_attributes_的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试建立一个表单,该表单的上一个居所关系上有嵌套字段,但是我遇到了批量分配错误.到目前为止,我的代码如下(已删除一些html):

I am currently trying to set up a form with nested fields on a belongs_to relationship, but I am running into a mass assignment error. My code so far is as follows (some html removed):

销售模式:

class Sale < ActiveRecord::Base
  attr_accessible :customer_attributes
  belongs_to :customer
  accepts_nested_attributes_for :customer
end

new.html.erb:

new.html.erb:

<div class="container">
  <%= form_for :sale, :url => sales_path do |sale| -%>
    <%= sale.fields_for :customer do |customer_builder| %>
      <%= render :partial => "customers/form", :locals => {:customer => customer_builder, :form_actions_visible => false} %>
    <% end -%>
  <% end -%>

customers/_form.html.erb

customers/_form.html.erb

<fieldset>
  <label class="control-label">Customer Type</label>
  <%= collection_select(:customer, :customer_type_id, CustomerType.all, :id, :value, {}, {:class => "chzn-select"}) %>
</fieldset>

我认为这应该允许我创建一个Sale对象和一个嵌套的Customer对象.发送的参数是(请注意包括一些不相关的参数):

I believe this should allow me to create a Sale object, and a nested Customer object. The parameters being sent are (note some unrelated params are included):

{"utf8"=>"✓",
"authenticity_token"=>"qCjHoU9lO8VS060dXFHak+OMoE/GkTMZckO0c5SZLUU=",
"customer"=>{"customer_type_id"=>"1"},
"sale"=>{"customer"=>{"features_attributes"=>{"feature_type_id"=>"1",
"value"=>"jimmy"}}},
"vehicle"=>{"trim_id"=>"1",
"model_year_id"=>"1"}}

我得到的错误是:

Can't mass-assign protected attributes: customer

我可以理解为什么会这样,因为:customer不在attr_accessible列表中出售-尽管表单不应该发送customer_attributes而不是Customer吗?

I can see why this might be the case, since :customer is not in the attr_accessible list for Sale - though shouldn't the form be sending customer_attributes instead of customer?

任何帮助/建议都值得赞赏.

Any help / advice appreciated.

据我所知,Sale模型中的attr_accessible应该包含:customer_attributes-如果有人说不同,请告诉我.

EDIT 1: As far as I can tell, attr_accessible in the Sale model should be covered with :customer_attributes - if anyone says different, please let me know.

我尝试了各种排列,但是我似乎无法获得发送customer_attributes的参数,而不是简单地发送客户-也许我错过了标签或在上面的表格中的某处使用了不正确的标签?

EDIT 2: I have tried various permutations, but I can not seem to get the parameters to send customer_attributes instead of simply customer - perhaps I have missed a tag or used an incorrect tag somewhere in the forms above?

我在SO上发现了另一个问题,该问题表明form_for标记中的:url =>部分存在问题-该问题是指格式化设置,但我想知道这是否是导致该问题的原因.这里有问题吗?

EDIT 3: I have found another question on SO that indicated a problem with the :url => part on the form_for tag - the question was referring to a formtastic setup, but I'm wondering if that could be what is causing the problem here?

推荐答案

我最终在这里得到了答案.关键是这一行:

I got to the answer here eventually. The key was this line:

<%= collection_select(:customer, :customer_type_id, CustomerType.all, :id, :value, {}, {:class => "chzn-select"}) %>

需要更改为:

<%= customer.collection_select(:customer_type_id, CustomerType.all, :id, :value, {}, {:class => "chzn-select"}) %>

一旦改变了,一切就到位了.

Once this was changed, everything fell into place.

这篇关于Rails-质量分配错误的Accepts_nested_attributes_的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆