如何在页面上显示 acts_as_taggable 标签,并在 Rails 中每个标签下方显示帖子?

How to display acts_as_taggable tags on a page with the posts underneath each tag in Rails?

我正在使用gemacts_as_taggable_on。我已将其设置并正常工作。我正在尝试创建一个页面,列出所有标签以及它们所属的每个标签下面的帖子?

(标记 1)

(标记 2)

(标签 3)

这是我的代码,我在一页上显示了所有标签,但我无法让帖子显示在标签下方?

标签控制器:

def index
    @tag   = current_user.owned_tags
    @posts = current_user.posts.tagged_with(@tag.name)
end

标签索引视图:

<div class="tag-lists">
    <% @tag.each do |tag| %>

        <div><%= tag.name %></div>

        <% @posts.each do |post| %>
            <div><%= post.title %></div>
        <% end %>

    <% end %>
</div>

您需要迭代每个标签的每个帖子

在您的控制器上:

def index
    @user  = current_user
    @tags  = @user.owned_tags
end    

您认为:

<div class="tag-lists">
    <% @tags.each do |tag| %>

        <div><%= tag.name %></div>

        <% @user.posts.tagged_with(tag.name).each do |post| %>
          <div><%= post.title %></div>
        <% end %>

    <% end %>
</div>