Check_box_tag

Hi,
I am new to Ruby on Rails. I am trying to use ‘check_box_tag’ to
delete multiple tags(objects) at a time. I want to store checked tag_ids
into a hash. And I should be able to delete all checked tags by clicking
on ‘Click Here’ link. But it is not storing tag_ids into hash.

I have attached the image file. Please find it. And also help me.

Thanks

<%- @tags.each_with_index do |g, i| -%>
<% unless g.id.blank? %>
<tr <%= ‘class=“even_row”’ if i.even? %>>

      <td><%= check_box_tag 'tag_ids[]', g.tag_id  %></td>
<td><%= g.tag.s_tag_id %></td>
      <td><%= link_to( g.tag.s_uf_name, :controller =>

“refrigirators”, :action => “edit_tag”, :id => g.tag_id ) %>

 <% if g.shelf_id.blank? %>
    <td> <%= "(none)" %> </td>
 <% else %>
      <td> <%= g.shelf.shelf_name %> </td>
 <% end %>

  </tr>
<% end %>
<%- end -%>

On Wed, Feb 15, 2012 at 5:36 AM, Ajit T. [email protected] wrote:

   <td> <%= "(none)" %> </td>

Could you please put the code of your “click here”?.
You have a destroy method in your controller, all you have to do is use
that array and destroy each element

def destroy

 params[:tag_ids].each do |t|
    Tag.find(t).destroy
 end

end

that should work, but I’m not sure if you just can do this

     Tag.find(params[:tag_ids]).destroy

I haven’t destroy all of them, but it could work

Javier Q.

Hi Javier,
I think that this approach is correct but I guess that if you use
transaction it will be better for your system performance.
:smiley:

On Wed, Feb 15, 2012 at 8:37 AM, Bruno M. [email protected]
wrote:

Hi Javier,
I think that this approach is correct but I guess that if you use
transaction it will be better for your system performance.
:smiley:

For example when I want to add several items using checkboxes I add it
by
doing this

Model.another_model_ids = params[:ids]
(Product.category_ids = params[:ids]) for example

or just by doing

Model.another_models << AnotherModel.find(params[:id])

I saw the first one on a railscast and it seems to work when adding, but
I’ve never done something like deleting several items

Maybe my approach is incorrect :slight_smile:

I’m kind of new on rails and… I’ve never used transactions, you mean
this?
http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html

Javier Q.

Hi Javier,

Following is the controller-action which I am using to delete the tags.
But the issue is with the ‘tag_ids[]’ array. The array will be always
nil. So i am unable to delete any of the tags.

def delete_tag
@delete_tags = params[:tag_ids]
if request.delete?
@delete_tags.each do |del|
Tag.destroy(del)
end
end
redirect_to :action => :index, :tab => ‘tags’
end

When I click on the link, I am getting following error message:

NoMethodError in AdminController#delete_tag

You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each

Thank you all

On Wed, Feb 15, 2012 at 11:00 PM, Ajit T. [email protected]
wrote:

@delete_tags.each do |del|

You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each

What is the code for the link?

On Feb 15, 2012, at 11:02 PM, Javier Q. wrote:

def delete_tag
When I click on the link, I am getting following error message:

NoMethodError in AdminController#delete_tag

You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each

What is the code for the link?

And what do your checkboxes look like in your view? The name you give
them will determine how they show up in your params hash. And the form
tag you enclose them in will determine whether they even make it to the
controller at all.

Walter

Hi Javier,

Following is the controller-action which I am using to delete the tags.
But the issue is with the ‘tag_ids[]’ array. The array will be always
nil. So i am unable to delete any of the tags.

def delete_tag
@delete_tags = params[:tag_ids]
if request.delete?
@delete_tags.each do |del|
Tag.destroy(del)
end
end
redirect_to :action => :index, :tab => ‘tags’
end

When I click on the link, I am getting following error message:

NoMethodError in AdminController#delete_tag

You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each

Thank you all

Exactly,
Using Transactions will be save you some DB connections. It’s simple to
use
it, take a look better in that url… it explains well how to use it.
:smiley:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs