我有一个帖子模型和一个类别模型
我想在一个页面上按类别显示所有帖子的列表.
我正确地在每个模型中使用has和belongs_to.
我只是不知道如何在我看来展示它们.
我想要的是
分类名称
类别名称2
等等...
谢谢
在你的控制器中设置@categories:
@categories = Category.find(:all, :include => :posts) # you may specify your conditions here
# :include is needed to avoid a query on each "category.posts" call later in the view
Run Code Online (Sandbox Code Playgroud)
然后在视图中:
<% for category in @categories %>
<strong><%= category.name %></strong>
<ul>
<% for post in category.posts %>
<li><%= post.name %></li>
<% end %>
</ul>
<% end %>
Run Code Online (Sandbox Code Playgroud)