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? %>>
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
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
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.
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
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.