如何在帖子的index.html页面中添加评论列表?

时间:2014-08-11 10:04:42

标签: ruby-on-rails ruby ruby-on-rails-4

如何在帖子的index.html页面中添加评论列表?

这是我的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

及其帖子显示视图:

<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' %>

及其帖子索引视图:

<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 %>

post.rb:

class Post < ActiveRecord::Base
    has_many :comments
end

comment.rb:

class Comment < ActiveRecord::Base
    belongs_to :post
end

显示页面显示评论很好,但索引不能...... 它显示错误:未定义的方法`comments'为# 请帮帮我&gt;“&lt; 我是一个新的Ruby on Rails作家

2 个答案:

答案 0 :(得分:0)

而不是在评论中使用@post使用索引页面上的帖子。你有拼写错误。

<%= 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 %>

答案 1 :(得分:0)

<强>协会

如果您正在使用ActiveRecord association来链接&#34;您的PostComment模式,您就可以在索引中的每个.comments上致电post

需要注意的事项(这是您的错误的核心)是您必须在.comments的每个instance上调用Post方法。您目前正试图在不存在的@instance variable上调用它:

#app/views/posts/index.html.erb
<% @posts.each do |post| %>
   <% post.comments.each do |comment| %>
      <%= comment.body %>
   <% end %>
<% end %>

当然,这只能通过使用您已经设置的设置(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