Search code examples
ruby-on-railsrubytagslink-totag-cloud

How to link_to tag names from tag_cloud?


  <% tag_cloud(@tags, %w(css1 css2 css3 css4)) do |tag, css_class| %>
    <%= link_to tag.name.titleize, tag_path(tag), :class => css_class %>
  <% end %>

If a user then clicks on one of the tags from the tag_cloud he is taken to: http://0.0.0.0:3000/tags/1, which is then blank.

Whereas if the user goes to a show page and clicks on one of the tags there <%= raw @habit.tag_list.map { |t| link_to t.titleize, tag_path(t), class: 'label label-primary' } %> he is taken to: http://0.0.0.0:3000/tags/morning%20routine, which will list out all the instances of morning routine from any of the models.

routes.rb

get 'tags/:tag', to: 'pages#home', as: :tag

The tag_cloud is a multi model cloud so simply switching out @tags with @habit doesn't work.

Please let me know if you need further code or explanation :-] Keep the dream alive!


Solution

  • Got it! Took me awhile to figure out it only required a slight change:

      <% tag_cloud(@tags, %w(css1 css2 css3 css4)) do |tag, css_class| %>
        <%= link_to tag.name.titleize, tag_path(tag.name), :class => css_class %>
      <% end %>