[英]How can I add a comment list in my posts' index.html page?
How can I add a comment list in my posts' index.html page? 如何在帖子的index.html页面中添加评论列表?
It is my PostsController: 这是我的PostsController:
def index
@posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
@comments = @post.comments.all
@comment = @post.comments.build
end
and its my posts show view: 其我的帖子显示视图:
<p id="notice"><%= notice %></p>
<p>
<h3><%= @post.name %></h3>
</p>
<p>
<%= (@post.descriptopm).html_safe %>
</p>
<%= link_to 'Edit', edit_post_path(@post), :class => "btn btn-info btn-xs" %>
<%= link_to 'Back', posts_path, :class => "btn btn-info btn-xs" %>
<h3>Comments</h3>
<% @comments.each do |comment| %>
<div>
<strong><%= comment.user_name %></strong>
<br />
<p><%= (comment.body).html_safe %></p>
</div>
<% end %>
<%= render 'comments/form' %>
and its my posts index view: 及其我的帖子索引视图:
<h1>Listing posts</h1>
<%= link_to 'Create a New Post', new_post_path, :class => "btn btn-success btn-sm" %>
<% @posts.each do |post| %>
<div class="post thumbnail">
<h3><%= post.name %></h3>
<div><%= (post.descriptopm).html_safe %></div>
<div class="bottom-bottoms">
<%= link_to 'Display', post, :class => "btn btn-info btn-xs" %>
<%= link_to 'Edit', edit_post_path(post), :class => "btn btn-info btn-xs" %>
<%= link_to 'Delete', post, method: :delete, data: { confirm: 'Are you sure?' }, :class => "btn btn-info btn-xs" %>
</div>
<h3>Comments</h3>
<% @post.comments.each do |comment| %>
<div>
<strong><%= comment.user_name %></strong>
<br />
<p><%= (comment.body).html_safe %></p>
</div>
<% end %>
<%= render 'comments/form' %>
</div>
<% end %>
the post.rb : post.rb:
class Post < ActiveRecord::Base
has_many :comments
end
the comment.rb : comment.rb:
class Comment < ActiveRecord::Base
belongs_to :post
end
the show page show the comments well but the index cannot... it shows the error : undefined method `comments' for # please help me >"< I am a newly Ruby on Rails writer 显示页面很好地显示了注释,但索引无法...显示错误:#的未定义方法“ comments”,请帮助我>“ <<我是一名新的Ruby on Rails作家
instead of using @post in comment use post on index page. 而不是在评论中使用@post,而是在索引页面上使用post。 You have typo error. 您输入错误。
<%= link_to 'Create a New Post', new_post_path, :class => "btn btn-success btn-sm" %>
<% @posts.each do |post| %>
<div class="post thumbnail">
<h3><%= post.name %></h3>
<div><%= (post.descriptopm).html_safe %></div>
<div class="bottom-bottoms">
<%= link_to 'Display', post, :class => "btn btn-info btn-xs" %>
<%= link_to 'Edit', edit_post_path(post), :class => "btn btn-info btn-xs" %>
<%= link_to 'Delete', post, method: :delete, data: { confirm: 'Are you sure?' }, :class => "btn btn-info btn-xs" %>
</div>
<h3>Comments</h3>
<% post.comments.each do |comment| %>
<div>
<strong><%= comment.user_name %></strong>
<br />
<p><%= (comment.body).html_safe %></p>
</div>
<% end %>
<%= render 'comments/form' %>
</div>
<% end %>
Associations 协会
If you're using ActiveRecord association to "link" your Post
and Comment
model, you'll be able to call .comments
on each post
you have in your index 如果您使用ActiveRecord关联来“链接”您的Post
和Comment
模型,则可以在索引中的每个post
上调用.comments
Something to note (and this lies at the core of your error) is that you will have to call the .comments
method on each instance
of the Post
. 需要注意的一点(这是错误的核心)是,您将必须在Post
每个instance
上调用.comments
方法。 You're currently trying to call it on an @instance variable
which doesn't exist: 您当前正在尝试在不存在的@instance variable
上调用它:
#app/views/posts/index.html.erb
<% @posts.each do |post| %>
<% post.comments.each do |comment| %>
<%= comment.body %>
<% end %>
<% end %>
This, of course, is only possible by using the setup you have already ( has_many
/ belongs_to
): 当然,这只能通过使用已经存在的设置( has_many
/ belongs_to
)来实现:
#app/models/post.rb
Class Post < ActiveRecord::Base
has_many :comments #-> post.comments
end
#app/models/comment.rb
Class Comment < ActiveRecord::Base
belongs_to :post #-> comment.post
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.