How to add a checkbox for each row in Rails 3.2 index page?

Posted by user938363 on Stack Overflow See other posts from Stack Overflow or by user938363
Published on 2014-06-06T02:27:20Z Indexed on 2014/06/06 3:24 UTC
Read the original article Hit count: 219

We would like to add a checkbox to each row on Rails index page to flag for the row. This checkbox is not part of the object (no checkbox boolean in database). When the index page shows, a user can check the box to trigger an event for the row in following process:

#objects/checkbox_index.html.erb    
<table>
  <tr>
    <th>CheckBox</th>
    <th>Object Name</th>
    <th>Object ID</th>
  </tr>
  <%= @objects.each do |obj| %>
    <tr>
      <td><%= checkbox %></td>
      <td><%= obj.name %></td>
      <td><%= obj.id %></td>
    </tr>
  <% end %>
</table>

In controller, the process will be like this:

@objects.each do |obj|
  some_event if obj.checked
end

There are a couple of questions we don't quite understand:

1. How to declare an array checkbox variable on the form and link it to each row of obj? We have been using `attr_accessor` to declare var for a form. 
2. How to retrieve each row on checkbox_index form and pass them back to controller?  We are using simple_form for new/edit. 

Can anyone point me towards any good examples of this sort of behavior, or suggest what we should be thinking about? Many Thanks.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about ruby-on-rails