現在、私はそれぞれの投稿を表示してそのループ内にループして、関連するすべての返信を表示しています。しかし現在、すべての返信はすべての掲示板のすべての投稿の下に表示されています。返信のみを投稿する
各投稿に関連する返信のみを表示するにはどうすればよいですか?
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
ビュー/ボード/ショー:
<% @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:
<% @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
@Shaniは投稿を更新しました – aidiah
@aidiah広告あなたの投稿コードと返信がどこに表示されているのかを質問するビューコード。 –