ActionController :: ParameterMissing和表中的多选择行

时间:2018-09-01 16:42:57

标签: ruby-on-rails

我试图在View中创建一个按钮,该按钮将创建一个新记录并显示在另一个控制器中,然后显示该View。

但是,当我单击该按钮时出现此错误。

ActionController::ParameterMissing in JobHeadersController#create
param is missing or the value is empty: job_header

# Never trust parameters from the scary internet, only allow the white list through.
def job_header_params
  params.require(:job_header).permit(:company_id, :customer_id, :name, :del_address1, :del_address2, :del_address3, :del_address4, :del_postcode, :tel, :created_by)
end
end
Parameters:

{"utf8"=>"✓",
"authenticity_token"=>"BLAHBLAHBLAHg==",
"company_id"=>"2",
"customer_id"=>"1",
"name"=>"Nash Labs",
"del_address1"=>"23 Taylor Road",
"del_address2"=>"Odsal",
"del_address3"=>"Bradford",
"del_address4"=>"West Yorkshire",
"del_postcode"=>"BD6 1BH",
"tel"=>"07522189605",
"created_by"=>"2",
"button"=>""}

我的查看代码是

 <%= form_tag({controller: "job_headers", action: "create"}, method: "post")  %>


  <% @other_assets.each do |assets| %>
    <tr>
      <td><%= check_box_tag 'selected_assets[]', assets.id%></td>
      <td><%= assets.service_type %></td>
      <td><%= assets.asset_type %></td>
      <td><%= assets.make %></td>
      <td><%= assets.model %></td>
      <td><%= assets.serial_no %></td>
      <td><%= format_date(assets.date_next) %></td>
      <td><%= link_to "Notes", customer_asset_service_register_path, :id => 'notesModal', "data-toggle" => "modal", 'data-target' => '.bs-example-modal-lg' %></td>

     </tr>
  <% end %>
 </table>


      </div>  
        <%= hidden_field_tag  :company_id, @customer.company_id %>
        <%= hidden_field_tag  :customer_id, @customer.id %>
        <%= hidden_field_tag  :name, @customer.name %>
        <% if @customer.del_address1.blank? %>
          <%= hidden_field_tag  :del_address1, @customer.address1 %>
        <% else %>
          <%= hidden_field_tag  :del_address1, @customer.del_address1 %>
        <% end %>
        <% if @customer.del_address1.blank? %>
          <%= hidden_field_tag  :del_address2, @customer.address2 %>
        <% else %>
          <%= hidden_field_tag  :del_address2, @customer.del_address2 %>
        <% end %>
        <% if @customer.del_address1.blank? %>
          <%= hidden_field_tag  :del_address3, @customer.address3 %>
        <% else %>
          <%= hidden_field_tag  :del_address3, @customer.del_address3 %>
        <% end %>
        <% if @customer.del_address1.blank? %>
          <%= hidden_field_tag  :del_address4, @customer.address4 %>
        <% else %>
          <%= hidden_field_tag  :del_address4, @customer.del_address4 %>
        <% end %>
        <% if @customer.del_address1.blank? %>
          <%= hidden_field_tag  :del_postcode, @customer.postcode %>
        <% else %>
          <%= hidden_field_tag  :del_postcode, @customer.del_postcode %>
        <% end %>

        <%= hidden_field_tag  :tel,  @customer.tel %>
        <%= hidden_field_tag  :created_by,  current_user.id %>

         <div class="box-tools pull-right">
            <%= button_tag(type: "submit", class: "btn btn-primary pull_right") do %>
                 <i class='fa fa-calendar'></i> Create Job
                  <% end %>

         <% end %>

如果我正确地读取了错误,则期望参数为“ job_header”,但是该参数为null,这是为了创建新的job_header记录,因此我无法传递该参数?

如果有人也知道如何在表格行上放置一个复选框,以便将选定的@other_asset ID作为数组传递给参数,请也告诉我。它将为我省去很多谷歌搜索机会。

预先感谢

2 个答案:

答案 0 :(得分:0)

使用方法:使用form_for @job_header do |f| 创建以@job_header为范围的表格,并在job_header_params中将为:

{"utf8"=>"✓", "authenticity_token"=>"BLABLABLA",
  "job_header": { 
    "company_id"=>"2",
    "customer_id"=>"1",
    "name"=>"Nash Labs",
    "del_address1"=>"23 Taylor Road",
    "del_address2"=>"Odsal",
    "del_address3"=>"Bradford",
    "del_address4"=>"West Yorkshire",
    "del_postcode"=>"BD6 1BH",
    "tel"=>"07522189605",
    "created_by"=>"2"},
"button"=>""}

例外是说缺少所需的节点(:job_header),并且可以找到允许的参数(params['job_header'] => nil)。

更多{@ {3}}

请注意以下未得到的事项: https://api.rubyonrails.org/v5.1/classes/ActionView/Helpers/FormHelper.html https://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html

如果您已经拥有这些隐藏字段的信息(在@customer中),为什么要发送到视图以从中接收““”“相同”“”信息? (与“相同,因为用户可以通过页面检查来更改此数据并发送恶意代码...) 只需在控制器上填写这些属性。

答案 1 :(得分:0)

  

JobHeadersController#create中的ActionController :: ParameterMissing

     

参数丢失或值为空:job_header

您正在使用form_tag提交数据,因此{{1}不会有任何模型实例) }追加。您只需要更改

params

params.require(:job_header).permit(:company_id, :customer_id, :name, :del_address1, :del_address2, :del_address3, :del_address4, :del_postcode, :tel, :created_by)