How do I create 'remove tag' links with Acts_As_Taggable_On in Rails?
I'm creating a simple project tagging application using the Act_As_Taggable_On gem.
Adding projects, and adding tags (in my case 'types' / 'type_list') to each project works great. Where I'm stuck is how to remove individual tags using Act_As_Taggable_On. What I would like is to be able to click the 'x' text link next to each tag (see link) to remove the tag from that projects type_list.
I've searched the documentation and found a method along the lines of:
But what I need help with is how to call the remove method on the specific tag, especially since the whole thing is being iterated with .each do
My controller and model code is pretty minimal and standard - based on Act_As_Taggable_On docs. Here is my view code for generating the layout above:
<h1><%= @title %></h1> <div class="column-left"> <% @projects.each do |project| %> <div class="p_wrapper"> <table> <tr> <td><div class="project p_name"><%= project.name %></div></td> <td><div class="p_link"><%= link_to 'Edit', edit_project_path(project) %></div></td> <td><div class="p_link"><%= link_to 'Nuke', project, :confirm => 'Are you sure?', :method => :delete %></div></td> </tr> </table> <table> <tr> <td> <% project.type_list.each do |tag|%> <div class="p_tag"> <%= tag %> <%= link_to "x", # %> <!-- THIS IS THE PART I'M STUCK ON --> </div> <% end %> </td> </tr> </table> <table> <tr> <td> <%= form_for(project) do |f| %> <%= f.text_field :inject_tags %> <%= f.submit "Add Tag" %> <% end %> </td> </tr> </table> </div> <% end %> <br /> <%= link_to 'Add new project', new_project_path %> </div>
Can anyone point me in the right direction? Am I implementing this correctly to be able to actually remove tags as described?
I would simply add a new method to your projects controller, like so:
def remove_tag Project.find(params[:id]).type_list.remove(params[:tag]) end
And in your routes file
resources :projects do member do put 'remove_tag', :as => :remove_tag end end
And in your view
<%= link_to 'x', remove_tag_project_path(project), :tag => tag, :method => :put %>
Of course you should add some sanitation, but it should work this way...