Rails嵌套表单 - 未允许的参数

时间:2017-05-16 22:54:47

标签: ruby-on-rails

我有一个Orders模型,它有很多项目和客户。出于某种原因,我在解决方案中看起来很高和很低,在使用嵌套表单时,我无法在其尊重的表中保存Items和Customers的属性。

我对Rails相对较新,所以如果我错过了一些明显的东西,我会道歉。

  

开始发布" / orders" for :: 1 at 2017-05-16 16:12:17 -0600处理   by OrdersController #create as HTML参数:{" utf8" =>"✓",   " authenticity_token" = GT;" k1QPQ560j44Mx0SNeCMBQAUGLQfEi4g0QpDfHuYeP4Zd7nVgcHJf3qXNsVQv29dV + 5G0oJ7wiaoQ3Idw + DP + IW ==&#34 ;,   "订单" => {"客户" => {"名称" =>"测试","电子邮件&# 34; =>"试验&#34 ;,   "电话" =>"测试"},"项目" => {"名称" =>"测试&# 34;," ref_num" =>"测试",   "零售" =>" 122"},"状态" =>"测试","到达" = >" 06/06/19 34#;,   "追踪" =>" Test439"},"提交" =>"保存订单"}未经许可   参数:customer,item

订单控制器

    def create
        @order = Order.new(order_params)
        @order.items.build
        @order.customers.build
        @order.save
        redirect_to @order 
    end
private
    def order_params
    params.require(:order).permit(:status, :arrival, :tracking, 
        customers_attributes: [:name, :phone, :email], 
        items_attributes: [:name, :ref_num, :retail])
    end

订单模型

class Order < ApplicationRecord
    has_many :items
    has_many :customers
    accepts_nested_attributes_for :items, :customers
end

订单表格(新)

<%= form_for :order, url: orders_path do |f| %>

<%# customer parameters%>
<%= f.fields_for :customers do |cu| %>
   <p>
    <%= cu.label :customer_name %><br>
    <%= cu.text_field :name %>
  </p>

  <p>
    <%= cu.label :email %><br>
    <%= cu.text_field :email %>
  </p>

  <p>
    <%= cu.label :phone %><br>
    <%= cu.text_field :phone %>
  </p>
<% end %>

 <%# Item parameters %>

 <%= f.fields_for :items do |it| %>

  <p>
    <%= it.label :item_name %><br>
    <%= it.text_field :name %>
  </p>

  <p>
    <%= it.label :reference_number %><br>
    <%= it.text_field :ref_num %>
  </p>

  <p>
    <%= it.label :retail %><br>
    <%= it.text_field :retail%>
  </p>
<% end %>

<%# order parameters%>
  <p>
    <%= f.label :status %><br>
    <%= f.text_field :status %>
  </p>

  <p>
    <%= f.label :est_arrival %><br>
    <%= f.text_field :arrival %>
  </p>

 <p>
    <%= f.label :tracking %><br>
    <%= f.text_field :tracking %>
</p>


  <p>
    <%= f.submit %>
  </p>

 <% end %>
<%= link_to 'Back', orders_path %>

1 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为form_for :order没有引用您的模型对象,它创建了一个简单的:order参数,它没有考虑您的模型。 accepts_nested_attributes_for,因此不会使用f.fields_for :customers后缀构建_attributes。您应该为Order中的预期行为实例化一个form_for @object对象,而不是仅引用仅呈现该参数名称的:object