Always I am indebted.

I'm currently creating a portfolio site where I can post articles like a blog, and implemented a tagging function using a gem called acts-as-taggable-on.
I want to list the tags associated with each article on the top page, and click the tag to display the associated articles.

The problem i am having

You can link tags when posting an article, and click the tag of the article from the detail page of the article to narrow down the display.
However, if you list the tags registered on the top page, even if you click the tags, you will not be able to narrow down properly and all articles will be displayed.

Corresponding source code


class Post</pre>
<p><br />
<pre><code>class PostsController</pre>
<pre><code><% = form_with model: @post, local: true do | form |%><% = render'layouts/error_messages', model: form.object%>       <% = form.label: Featured image%><% = form.file_field: image, id: "item-image", accept: "image/png, image/jpeg, image/gif", class: "form-control"%>     <% = form.label: Title, class: "h5"%><% = form.text_field: title, class: "form-control", placeholder: "Title (required up to 40 characters)", maxlength: "40"%>      <% = form.label: Article, class: "h5"%><% = form.rich_text_area: content%>          <% = form.label: tag, class: "h5"%>  <% = form.text_field: tag_list, value: @ post.tag_list.join (','), placeholder: "When specifying multiple tags, separate them with a comma (,)", class: "form-control" "%>      <% = form.submit "submit", class: "form-control btn btn-outline-secondary"%><% end%>


Make the connection between the earth and people more familiar.   <p>Let's connect our daily lives to the future with everyone's posts</p><br><hr><% breadcrumb: root%>          <% @ posts.each do | post |%>              <% = image_tag post.image.variant (resize: '300x400'). processed, class: "card-img-top"%>                   <% = post.title%>         <p><% = strip_tags (post.content.to_s) .gsub (/ [\ n] /, ""). strip.truncate (20)%></p>                <% = render partial:'posts/posts', locals: {post: post}%>              " ,,>    <% if post.user.image.attached?%>      <% = image_tag post.user.image, class: "avatar m-1"%>    <% else%>      <% = image_tag ("defo.jpg"), class: "avatar m-1"%>    <% end%>              <p><% = post.user.nickname%></p>      <span>Post date:<% = post.created_at.to_s (: datetime_jp)%></span>                           Post date:<% = post.created_at.to_s (: datetime_jp)%>                      <% = link_to'Detail page', post, class: "btn btn-outline-secondary form-control"%>                                  <% end%>                       Tag list      Click to see related articles<% @ tags.each do | tag |%><span><% = link_to tag.name, posts_path (tag_name: tag.name)%>  </span><% end%>   Card link      Another link           <% = paginate @posts%>


<% tag_list.each do | tag |%><span><% = link_to tag, posts_path (tag_name: tag), class: "text-white"%></span><% end%>
What I tried

The URL when clicking the tag list on the top page to jump to the link destination is
"Http: // localhost: 3000/posts? tag_name = 4"
The tag is displayed as a number in, but the URL of the link destination of the tag that has been narrowed down normally is "http: // localhost: 3000/posts? Tag_name = cat" in the data base I understand that the way the stored data is called is different. So, change the index of posts_controller.rb

  @tags = Post.tag_counts_on (: tags) .order ('count DESC')

Part of

  @tags = Post.tag_list_on (: tags) .order ('count DESC')

I changed it to and tried it, but I got the error undefined method `tag_list_on'for #.

After that, I tried changing the instance variable in the same place.

Change before

<% @ tags.each do | tag |%><span><% = link_to tag.name, posts_path (tag: tag.name)%>  </span><% end%>

After change

<% @ posts.each do | post |%><span><% = link_to tag, posts_path (tag: tag.name)%>  </span><% end%>

I get the error undefined method `name'for #.

Official reference: acts-as-taggable-onI also read it, but I couldn't get any information that would lead to a solution because I was a beginner myself.

I searched for other things, but I couldn't think of a solution myself, so I asked a question.

I would appreciate it if you could teach me the appropriate method.

Thank you.

Supplementary information (FW/tool version, etc.)

ruby_version 2.6.5


This implementation isThis articleI referred to.

  • Answer # 1

    In views/posts/_tag_list.html.erb
    <% = link_to tag, posts_path (tag_name: tag),However, it seems that this partial is not used, and it is used
    <% = link_to tag, posts_path (tag: tag.name)%>It seems like.
    in spite of
    Post.tagged_with ("# {params [: tag_name]}")That's why
    Post.tagged_with (params [: tag])Please

Related articles