HABTM复选框未经许可的参数

时间:2014-07-14 03:04:16

标签: ruby-on-rails ruby-on-rails-4 has-and-belongs-to-many

我无法以偏远的形式保存选定的框。

我需要选择一些公司并保存在邀请表中。

作为印刷品: enter image description here

我尝试过添加permited params company_ids:[],company_ids => []和{company_ids => []} ..没有任何作用..

远程表单:

<%= form_tag @invitations, remote:true do |f| %>
  <table class="table">
    <thead>
        <tr>
            <th></th>
            <th>Nome Fantasia</th>
            <th>Razão Social</th>
            <th>CNPJ</th>
            <th>Estado</th>
            <th>Cidade</th>
            <th>Ramo de atividade</th>
        </tr>
    </thead>
    <tbody>
        <%= hidden_field_tag "invitation[company_ids][]", nil %>
        <% @companies.each do |company| %>
            <tr>
                <td><%= check_box_tag "invitation[company_ids][]", company.id, id: dom_id(company) %></td>
                <td><%= company.nomefantasia %></td>
                <td><%= company.razaosocial %></td>
                <td><%= company.cnpj %></td>
                <td><%= company.state.estado %></td>
                <td><%= company.city.cidade %></td>
                <td><%= company.category.nome %></td>
            </tr>
        <% end %>
      </tbody>
    </table>
</div>
        <%= button_tag(:type => 'submit', :class => 'btn btn-success btn-lg') do %>
                    Enviar <i class="fa fa-send-o"></i>
        <% end %>
    <% end %>

模型控制器创建 Invitations_Controller.rb

    def invitation_params
params.require(:invitation).permit(:representative_id,:company_id,:user_id,:quote_id,company_ids: [] )
    end

消息错误:

Started POST "/invitations" for 127.0.0.1 at 2014-07-14 00:29:21 -0300
Processing by InvitationsController#create as JS
  Parameters: {"utf8"=>"✓", "invitation"=>{"company_ids"=>["", "2", "3"]}}
Unpermitted parameters: company_ids
   (0.3ms)  BEGIN
  SQL (0.4ms)  INSERT INTO `invitations` (`created_at`, `updated_at`) VALUES ('2014-07-14 03:29:21', '2014-07-14 03:29:21')
   (81.3ms)  COMMIT
Redirected to http://0.0.0.0:3000/invitations/7
Completed 302 Found in 89ms (ActiveRecord: 82.0ms)

1 个答案:

答案 0 :(得分:0)

我认为@Pavan是对的 -


你的参数很好看:

Parameters: {"utf8"=>"✓", "invitation"=>{"company_ids"=>["", "2", "3"]}}

这意味着当您拨打strong_params方法时,您必须拨打以下电话:

params.require(:invitation).permit(company_ids: [])

-

由于您只是更新关联的HABTM数据,为什么不尝试仅将strong_params方法更新为 接受company_ids属性?

-

除此之外,我认为你的系统看起来还不错