简体   繁体   English

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

[英]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关联来“链接”您的PostComment模型,则可以在索引中的每个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.

相关问题 如何在我的帖子索引页面上添加复选框过滤器(Rails应用) - How do I add checkbox filters to my Posts index page (Rails app) 无法让Heroku服务于公共index.html页 - Can't get Heroku to serve public index.html page 如何过滤这些帖子然后将它们添加回列表? - How can I filter through these posts then add them back into a list? Ruby On Rails:如何创建到public / index.html静态文件的路由? - Ruby On Rails: How can I create a route to the public/index.html static file? 如何重定向到 root - public/index.html? - How do I redirect to root - public/index.html? 如何为样式目的将索引页上的帖子分隔为单独的元素? - How to make posts on my index page seperate elements for styling purposes? 我只想在Rails应用程序的索引页面中显示用户创建的帖子,而不是所有帖子 - i only want to display posts created by user in index page in my rails app instead of all posts 一个注释html表单,可在单个页面中包含多个帖子(DRY建议) - One comment html form for many posts in single page (DRY advice) 如何在ActiveAdmin索引页面中的记录中添加工具提示? - How can I add a Tooltip to a record in ActiveAdmin index page? 我如何评论自己的帖子? - How can I comments to my posts?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM