显示特定类别的所有帖子,目前只返回一个结果 - Rails 5
Display ALL posts with specific category, only returning one result so far- Rails 5
我正在使用
rails (5.1.4)
并尝试显示所有标记有特定类别的 post。我目前只能 return 一个 post 而不是每个 post 这个类别。
Post.rb
has_many :post_categories
has_many :categories, through: :post_categories
extend FriendlyId
friendly_id :title, use: :slugged
def category_list
categories.map(&:name)
end
Category.rb:
class Category < ApplicationRecord
has_many :post_categories
has_many :posts, through: :post_categories
extend FriendlyId
friendly_id :name, use: :slugged
end
类别控制器:
def show
@category = Category.find_by_name(params[:category])
@posts = @category.posts
end
Category#show ERB:(我试图用这个类别列出每个 post,但只得到一个结果,而我实际上可以看到许多 post 具有相同的结果类别)
<% @posts.each do |post| %>
<h1 class="title"><%= link_to post.title, post_path(post) %></h1>
<% end %>
从终端:
Started GET "/category/Philadelphia%20maki" for 127.0.0.1 at 2017-12-17 21:03:39 -0800
Processing by CategoriesController#show as HTML
Parameters: {"category"=>"Philadelphia maki"}
Category Load (5.6ms) SELECT "categories".* FROM "categories" WHERE "categories"."name" = LIMIT [["name", "Philadelphia maki"], ["LIMIT", 1]]
Rendering categories/show.html.erb within layouts/dashboards
Post Load (2.0ms) SELECT "posts".* FROM "posts" INNER JOIN "post_categories" ON "posts"."id" = "post_categories"."post_id" WHERE "post_categories"."category_id" = [["category_id", 15]]
路线:
category GET|POST /category/:category(.:format) categories#show
帖子#show ERB:
<ul class="category-list">
<% @post.category_list.map.each do |category| %></p>
<li itemprop="genre" class="category-list-item"> <%= link_to "#{category}", category_path(category) %></li>
<% end %>
</ul>
对于访问此页面的用户,
问题
用户在不同的页面上看到多个具有相同类别名称的 post,但在获取该类别的 post 时,它只返回 1 个 post。
原因
数据库中有多个 Category
条同名记录。
解决方案
向模型添加验证 Category
:
validates :name, uniqueness: true, allow_blank: true
我正在使用 rails (5.1.4) 并尝试显示所有标记有特定类别的 post。我目前只能 return 一个 post 而不是每个 post 这个类别。
Post.rb
has_many :post_categories
has_many :categories, through: :post_categories
extend FriendlyId
friendly_id :title, use: :slugged
def category_list
categories.map(&:name)
end
Category.rb:
class Category < ApplicationRecord
has_many :post_categories
has_many :posts, through: :post_categories
extend FriendlyId
friendly_id :name, use: :slugged
end
类别控制器:
def show
@category = Category.find_by_name(params[:category])
@posts = @category.posts
end
Category#show ERB:(我试图用这个类别列出每个 post,但只得到一个结果,而我实际上可以看到许多 post 具有相同的结果类别)
<% @posts.each do |post| %>
<h1 class="title"><%= link_to post.title, post_path(post) %></h1>
<% end %>
从终端:
Started GET "/category/Philadelphia%20maki" for 127.0.0.1 at 2017-12-17 21:03:39 -0800
Processing by CategoriesController#show as HTML
Parameters: {"category"=>"Philadelphia maki"}
Category Load (5.6ms) SELECT "categories".* FROM "categories" WHERE "categories"."name" = LIMIT [["name", "Philadelphia maki"], ["LIMIT", 1]]
Rendering categories/show.html.erb within layouts/dashboards
Post Load (2.0ms) SELECT "posts".* FROM "posts" INNER JOIN "post_categories" ON "posts"."id" = "post_categories"."post_id" WHERE "post_categories"."category_id" = [["category_id", 15]]
路线:
category GET|POST /category/:category(.:format) categories#show
帖子#show ERB:
<ul class="category-list">
<% @post.category_list.map.each do |category| %></p>
<li itemprop="genre" class="category-list-item"> <%= link_to "#{category}", category_path(category) %></li>
<% end %>
</ul>
对于访问此页面的用户,
问题
用户在不同的页面上看到多个具有相同类别名称的 post,但在获取该类别的 post 时,它只返回 1 个 post。
原因
数据库中有多个 Category
条同名记录。
解决方案
向模型添加验证 Category
:
validates :name, uniqueness: true, allow_blank: true