我设法将评论链接到他们在铁轨中的帖子,但剩下的就是显示它们,这是我的观点(简化):
问题是iam迭代post.all
和每个帖子has_many :comments
,每个评论都有comment.description
comment.image
所以我怎样才能显示每个迭代帖子的评论是它通过另一次迭代?
_profile.html.erb:
@posts.each do |x|
<div class="eachpost-wrapper">
<%= x.text %>
<%= x.picture %>
<div class="comments-for-each-post">
<<<<<<<<< the post comments are supposed to be here <<<<<<
</div>
</div>
<% end %>
帐户控制人:
def profile
@posts = User.find(params[:id]).posts
@comment = Comment.new
end
评论控制器:
def new
@comment = Comment.new
end
def create
@post = Post.find(params[:id])
@comment = @post.comments.new
if @comment.save
redirect_to "success/success"
else
redirect_to 'error/error404'
end
答案 0 :(得分:1)
是的,这是另一次迭代。有点像:
<div class="posts-wrapper">
<% @posts.each do |post| %>
<div class="post-wrapper">
<%= post.text %>
<%= post.picture %>
<div class="comments-wrapper">
<% post.comments.each do |comment| %>
<div class="comment-wrapper">
# do stuff with comment here...
</div>
<% end %>
</div>
<div class="new-comment-wrapper">
# put a new comment form in here...
</div>
</div>
<% end %>
</div>
记住Mr.Yoshiji所说的关于急切加载的内容......