5

How can I pass the value of array? of the selected checkbox.

In View:

= check_box_tag 'user_message_ids[]', user_message.id, false

= link_to "<button>Bulk Delete</button>".html_safe, profile_message_path(user_message), :id => 'user_message_ids', :confirm => "Are you sure?", :method => :delete

and can I place the submit button in any of this area.

like this one:

= form_tag checked_messages_path do
  = check_box_tag 'user_message_ids[]', user_message.id, false
--------objects---------------------------------------------
--------objects---------------------------------------------
--------objects---------------------------------------------
--------objects---------------------------------------------
= submit_tag "Delete Checked"

2 Answers 2

20

Use a form_tag block

<% form_tag delete_mutiple_items_path do %>
  <table>
    <thead>
      <tr>
        <th></th>
      </tr>
    </thead>
    <tbody>
      <% @items.each do |item| %>
      <tr>
        <td><%= check_box_tag "items[]", item.id %></td>
      </tr>
      <% end %>
    </tbody>
  </table>
  <%= submit_tag "delete Checked" %>
<% end %>

It will pass an array of ids to controller, like {"item_ids[]" => ["1", "2", "3"]}

So you can do anything with these ids

FYI: http://railscasts.com/episodes/165-edit-multiple?view=asciicast

Updated (One Small Gotcha)

From here: http://railscasts.com/episodes/17-habtm-checkboxes?view=asciicast

There is still one small problem with our update method. If we uncheck all of the checkboxes to remove a product from all categories then the update fails to remove any categories the product was in. This is because a checkbox on an HTML form will not have its value sent back if it is unchecked and therefore no category_ids will appear in the product’s parameters hash, meaning that the category_ids are not updated.

To fix this we have to modify our products controller to set the category_ids parameter to be an empty array if it is not passed to the update action. We can do this using Ruby's ||= operator and add the following like at the top of the update action.

params[:product][:category_ids] ||= []

This will ensure that if none of the checkboxes are checked then the product is updated correctly so that it is in no categories.

Sign up to request clarification or add additional context in comments.

3 Comments

Thnx, I think this may help me. GodBless
suggestions for Select and unselect all checkbox?
I see a problem here and it is that if none check_box is selected the update will not modify the entity, because the array param will not be sent.
1

You need to add the button tag around a form tag. You cannot submit input tag data using a link_to.

1 Comment

but I need to place the button on the top of objects and its not working... any more advice?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.