简体   繁体   中英

Route path for tags in two tables using acts_as_taggable gem

I'm using acts_as_taggable gem to add tags to two separate models Articles and Coffeeshops. I have the ability to submit and display the tags in the two models separately perfectly fine. When you click the tag it also shows you a list of articles containing just that tag which is great. What I can't work out is how to do this for the Coffeeshops. The path just takes you to the Articles index. I am certain I will need to change something in routes.rb and also the individual path in coffeeshop/index.html.erb

In routes I have :

get 'tags/:tag', to: 'articles#index', as: :tag

replicating this for coffeeshops obviously doesn't work. I'm thinking along the lines of:

 get 'tags/:article_tag, to 'articles#index, as: article_tag
 get 'tags/:coffeeshop_tag, to 'coffeeshops#index, as: coffeeshop_tag

get 'articles/tags/:article_tag, to 'articles#index, as: article_tag
get 'coffeeshops/tags/:coffeeshop_tag, to 'coffeeshops#index, as: coffeeshop_tag

Doesn't work either

But how do I change this in the view? I currently have: <%= raw(@article.tag_list.map { |t| link_to t, tag_path(t) }.join(', ')) %>

I changed this to:

<%= raw(@article.tag_list.map { |t| link_to t, @article.tag_path(t) }.join(', ')) %> but that only threw undefined method 'tag_path' for #<Article:0x007fd49ab18f98> Did you mean? image_path undefined method 'tag_path' for #<Article:0x007fd49ab18f98> Did you mean? image_path error.

Sorted it.

Updated view tags to:

<%= raw(@coffeeshop.tag_list.map { |t| link_to t, coffeeshops_tag_path(t) }.join(', ')) %> .

And altered the route as below:

get 'coffeeshops/tags/:tag', to: 'coffeeshops#index', as: :coffeeshops_tag

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM