如何将从复选框获取的值更改为SQL条件?

时间:2015-08-03 09:20:55

标签: ruby-on-rails ruby-on-rails-4

我使用check_box_tag来获取选定的信标,并使用collection_select来定义值。当用户按下按钮时,我希望所有选定的信标都将值更改为collection_select选择的值,但我不知道如何使用ids []来设置我的条件,如“where ... in ...”< /强>

这是我的行动,我希望params[:ids]改为[1,2,3]

@beacons = Beacon.where(:id => params[:ids]).update_all(:installer_id => params[:account][:installer_id])

这是我的表:

<table class="col-md-12 table-bordered table-striped table-condensed cf">

       <thead class="cf">
        <th>Check</th>
        <th>BeaconUUID</th>
        <th>BeaconCategory</th>
        <th>BeaconLocate</th>
        <th>BeaconName</th>
        <th>BeaconAddress</th>
        <th>Assign</th>
       </thead>
      <tbody>
          <% @beacons.each do |beacon| %>
            <tr>
              <td><%= check_box_tag "ids[]", beacon.id %></td>
              <td><%= beacon.beacon_uuid %></td>
              <td><%= beacon.cid %></td>
              <td><%= beacon.lid %></td>
              <td><%= beacon.beacon_name %></td>
              <td><%= beacon.beacon_address %></td>
              <% if beacon.installer_id.nil? %> 
                <td>not assign</td>
              <% else %>
                <td>assigned:<%= beacon.installer_id %></td>
              <% end %>

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

<%= form_tag assigned_beacons_path, :method => 'post' do %>
  <td><%= collection_select(:account, :installer_id, Account.where('manager_id = ?',session[:user_id]), :id, :email, {:prompt => 'choose!'}, :style => "width: 100px;") %></td>
  <td><p data-placement="top" data-toggle="tooltip" title="Edit"><%= submit_tag 'assign', class: "btn btn-info btn-xs"%></p></td>
<% end %>

1 个答案:

答案 0 :(得分:1)

正如评论中指出的那样,check_box_tag应该移到form_tag块中:

<%= form_tag assigned_beacons_path, :method => 'post' do %>
  <table class="col-md-12 table-bordered table-striped table-condensed cf">

         <thead class="cf">
          <th>Check</th>
          <th>BeaconUUID</th>
          <th>BeaconCategory</th>
          <th>BeaconLocate</th>
          <th>BeaconName</th>
          <th>BeaconAddress</th>
          <th>Assign</th>
         </thead>
        <tbody>
            <% @beacons.each do |beacon| %>
              <tr>
                <td><%= check_box_tag "ids[]", beacon.id %></td>
                <td><%= beacon.beacon_uuid %></td>
                <td><%= beacon.cid %></td>
                <td><%= beacon.lid %></td>
                <td><%= beacon.beacon_name %></td>
                <td><%= beacon.beacon_address %></td>
                <% if beacon.installer_id.nil? %> 
                  <td>not assign</td>
                <% else %>
                  <td>assigned:<%= beacon.installer_id %></td>
                <% end %>

              </tr>
          <% end %>
        </tbody>    
  </table>
  <td><%= collection_select(:account, :installer_id, Account.where('manager_id = ?',session[:user_id]), :id, :email, {:prompt => 'choose!'}, :style => "width: 100px;") %></td>
  <td><p data-placement="top" data-toggle="tooltip" title="Edit"><%= submit_tag 'assign', class: "btn btn-info btn-xs"%></p></td>
<% end %>

另外,悬空<td>块看起来不太好,你也应该将它们移到这个或另一个<table>中。