0

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.

Are there any good examples of this sort of behavior, or resources to suggest what we should be thinking about?

halfer
  • 19,824
  • 17
  • 99
  • 186
user938363
  • 9,990
  • 38
  • 137
  • 303
  • It seems like you would want to use Javascript, not Ruby, for listening to an event. Have you considered adding a javascript event listener to a checkbox that isn't attached to the form? – steel Jun 06 '14 at 02:46
  • Would like to hear more about the idea. – user938363 Jun 06 '14 at 03:06
  • 1
    I leave a answer for this, but i just wanna add that using attr_accessor for this type of things is bad idea, you don't have how to tell that your attr_accesor is a boolean so if you have values like *1* or *"blabla"* it will return true, on the other side the cases like *nil* will return false, there are gems for managing this cases, that will remember the original value, so you can determinate true for your attribute accesor and have just the two comparations, like if change or not, but it's excessive for what i understand you want, regards! – Alexis Jun 06 '14 at 03:30

3 Answers3

1

One solution to this would be to add a Javascript event listener to a checkbox that is not part of the form. If the action you want triggered is a database call, you can do that with ajax. Without sitting down and writing all of that javascript code, the Ruby and pseudo javascript code might look something like this:

Ruby

<% @objects.each do |obj| %>
    <%= check_box_tag, class: "checkbox" %>
<% end %>

Javascript/JQuery

$('#checkbox').on change {
    if selected == selected
        do what you are trying to do on $(this).element (like an ajax call)
    else
        undo what you did
    end
}
steel
  • 11,883
  • 7
  • 72
  • 109
1

for identifying each checkbox you should use <%= check_box_tag "object_ids[]", object.id %>, it will return to your controller and array with all the ids of the objects that you check on submit, check this video of Ryan Bates that is where i learn this technique, regards

Alexis
  • 4,836
  • 2
  • 21
  • 27
  • another thing, the only is outdate in that episode is the paths on the routes, so you should use something like in this [answer](http://stackoverflow.com/a/12584016/1802527), good luck – Alexis Jun 08 '14 at 12:48
1

If all you need is a way to get info / effect the row a checkbox was clicked on, then this may help.

JS:

$('yourTableHere').on('click', 'input', function(){
    $(this).parentsUntil('tr').toggleClass('active'); // any functionality can go here
});

Demo Fiddle

badAdviceGuy
  • 2,060
  • 1
  • 16
  • 12