Search code examples
ruby-on-railstagsacts-as-taggable-on

Linking to acts_as_taggable_on tags within a Post


I've got a very simple setup of posts with associated tags. When I 'show' a post I want to be able to link to each one of those tags BUT it seems to only link to the tag :id that shares the :id of the post I'm showing.

My code:

<% @post.tag_list.each do |tag| %>
    <%= link_to tag, tag_path() %>
<% end %>

Let's say I'm looking at post number 2, the above will only link me to /tags/2 , no matter which tag I click on. I'm sure the answer is embarrassingly simple but it's driving me crazy. Thanks so much.


Solution

  • Pass tag to your route helper:

    <% @post.tag_list.each do |tag| %>
      <%= link_to tag, tag_path(tag) %>
    <% end %>
    

    Update:

    Change tag_list to tags:

    <% @post.tags.each do |tag| %>
      <%= link_to tag, tag_path(tag) %>
    <% end %>