Ruby on rails 仅显示与帖子相关的回复
目前,我正在循环显示每一篇文章,并在该循环内循环显示所有相关回复。然而,目前所有的回复都显示在每个板块的每个帖子下 如何只显示与每篇文章相关的回复Ruby on rails 仅显示与帖子相关的回复,ruby-on-rails,ruby,Ruby On Rails,Ruby,目前,我正在循环显示每一篇文章,并在该循环内循环显示所有相关回复。然而,目前所有的回复都显示在每个板块的每个帖子下 如何只显示与每篇文章相关的回复 resources :boards, :path => '' do resources :posts, :path => 'thread' do resources :replies class BoardsController < ApplicationController def show @b
resources :boards, :path => '' do
resources :posts, :path => 'thread' do
resources :replies
class BoardsController < ApplicationController
def show
@board = Board.friendly.find(params[:id])
@boards = Board.all
@replies = Reply.all
end
class PostsController < ApplicationController
def show
@board = Board.friendly.find(params[:board_id])
@boards = Board.all
@replies = Reply.all
@post = Post.includes(:board).where('boards.slug' => params[:board_id]).friendly.find(params[:id])
end
class Board < ActiveRecord::Base
has_many :posts
has_many :replies, through: :posts
include FriendlyId
friendly_id :name, use: :slugged
accepts_nested_attributes_for :posts, :replies
end
class Post < ActiveRecord::Base
belongs_to :board
has_many :replies, dependent: :destroy
accepts_nested_attributes_for :replies
include FriendlyId
friendly_id :pid, :use => :scoped, :scope => :id
end
resources:boards,:path=>''do
资源:posts,:path=>'thread'do
资源:答复
类BoardsController<应用程序控制器
def秀
@board=board.friendly.find(参数[:id])
@boards=Board.all
@回复=Reply.all
结束
类PostsController<应用程序控制器
def秀
@board=board.friendly.find(参数[:board\u id])
@boards=Board.all
@回复=Reply.all
@post=post.includes(:board).where('boards.slug'=>params[:board_id]).friendly.find(params[:id])
结束
类板:scope,:scope=>:id
结束
视图/板/显示:
<% @board.posts.find_each do |post| %>
<%= post.subject %>
<%= post.name %>
<%= post.email %>
<%= post.created_at %>
No.<%= post.pid %>
<%= link_to "[reply]", board_posts_path(@board, @post)%>
<br>
<%= post.comment %><br><br>
<% render "replies/replies" %>
<% end %>
不
查看/回复/_回复:
<% @replies.each do |reply| %>
<p>
>><%= reply.name %>
<% reply.created_at %>
<%= reply.email %>
<%= reply.subject %>
<%= reply.created_at.strftime("%m/%d/%Y(%a) %I:%M:%S ") %>
No.<%= reply.pid %>
<br>
<%= reply.comment %>
</p>
<% end %>
>>
不
您不需要在控制器级别设置回复,因为回复属于帖子。您可以一次又一次地获得给定帖子的所有回复
在你看来
@posts.each do |post|
replies = post.replies // get all the replies that belongs to this post
replies.each do |reply|
reply.attribute
end
end
您不需要在控制器级别设置回复,因为回复属于帖子。您可以一次又一次地获得给定帖子的所有回复 在你看来
@posts.each do |post|
replies = post.replies // get all the replies that belongs to this post
replies.each do |reply|
reply.attribute
end
end
由于您只想在回复中显示相关回复,请不要使用通用的回复,请在此处使用
post
:
视图/板/显示:
。。。
查看/回复/_回复:
<% @replies.each do |reply| %>
<p>
>><%= reply.name %>
<% reply.created_at %>
<%= reply.email %>
<%= reply.subject %>
<%= reply.created_at.strftime("%m/%d/%Y(%a) %I:%M:%S ") %>
No.<%= reply.pid %>
<br>
<%= reply.comment %>
</p>
<% end %>
...
因为您只想在\u回复中显示相关回复
,所以不要使用通用的@回复
,请使用发布
:
视图/板/显示:
。。。
查看/回复/_回复:
<% @replies.each do |reply| %>
<p>
>><%= reply.name %>
<% reply.created_at %>
<%= reply.email %>
<%= reply.subject %>
<%= reply.created_at.strftime("%m/%d/%Y(%a) %I:%M:%S ") %>
No.<%= reply.pid %>
<br>
<%= reply.comment %>
</p>
<% end %>
...
您也可以粘贴您的型号代码吗?@Shani更新了post@aidiah将你的查看代码添加到广告中,询问你在哪里显示帖子和回复。你也可以粘贴你的模型代码吗?@Shani更新了post@aidiah广告您的查看代码,以询问您在哪里显示帖子和回复。