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 ) %></td>

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

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

Attachments:
http://www.ruby-forum.com/attachment/7060/checkboxtag.jpg


--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Javier Quarite at Feb 15, 2012 at 12:21 pm

    On Wed, Feb 15, 2012 at 5:36 AM, Ajit Teli wrote:

    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 ) %></td>

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

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

    Attachments:
    http://www.ruby-forum.com/attachment/7060/checkboxtag.jpg

    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.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Bruno Meira at Feb 15, 2012 at 1:37 pm
    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.
    :D

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Javier Quarite at Feb 15, 2012 at 2:05 pm

    On Wed, Feb 15, 2012 at 8:37 AM, Bruno Meira 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.
    :D
    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 :)

    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.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Bruno Meira at Feb 15, 2012 at 2:16 pm
    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.
    :D

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Ajit Teli at Feb 16, 2012 at 4:00 am
    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

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Javier Quarite at Feb 16, 2012 at 4:03 am

    On Wed, Feb 15, 2012 at 11:00 PM, Ajit Teli wrote:

    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
    What is the code for the link?

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Walter Lee Davis at Feb 16, 2012 at 4:33 am

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



    On Wed, Feb 15, 2012 at 11:00 PM, Ajit Teli wrote:
    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


    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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Ajit Teli at Feb 16, 2012 at 6:48 am
    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

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedFeb 15, '12 at 10:36a
activeFeb 16, '12 at 6:48a
posts9
users4
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase