如何显示所有评论?

时间:2015-10-23 11:30:24

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

请帮我显示某些帖子的所有评论。

我使用以下宝石: 'awesome_nested_set', 'acts_as_commentable_with_threading'

例如,我创建了脚手架'消息'。我尝试某些消息单元制作评论帖。

MessagesController:

def show
  # to display all comments
  @all_comments = @message.comment_threads  
  p '-----------------'
  p @all_comments
  p @all_comments.count

  # for form new comment
  @message = Message.find(params[:id])
  @user_who_commented = current_user
  @comment = Comment.build_from( @message, @user_who_commented.id, "Hey guys this is my comment!" )      
end

视图/消息/ show.html.erb:

<p>
  <strong>message Title:</strong>
  <%= @message.title %>
</p>

<p>
  <strong>message Body:</strong>
  <%= @message.body %>
</p>

<%= render 'comments/form' %>

<% @all_comments.each do |comment| %>
  <div>
    <%= @comment.title %>
    <%= @comment.body %>
  </div>
<% end %>

架构:

create_table "comments", force: :cascade do |t|
  t.integer  "commentable_id"
  t.string   "commentable_type"
  t.string   "title"
  t.text     "body"
  t.string   "subject"
  t.integer  "user_id",          null: false
  t.integer  "parent_id"
  t.integer  "lft"
  t.integer  "rgt"
  t.datetime "created_at"
  t.datetime "updated_at"
end
在通过create-action字段添加新评论i(和gem)之后,在此表中

title,
body,
user_id,
lft,
rgt

CommentsController:

def create
  comment = Comment.new(comment_params)
  comment.user = current_user
  comment.save

  if comment.update_attributes(user: current_user)
    redirect_to messages_path, notice: 'Comment was successfully created.' 
  else
    render :new 
  end
end

def new
  @comment = Comment.new
end

添加新邮件的表单工作正常,但不显示某些邮件的所有评论。

PS: 的登录

Started GET "/messages/1" for 127.0.0.1 at 2015-10-23 14:09:47 +0300
Processing by MessagesController#show as HTML
  Parameters: {"id"=>"1"}
  Message Load (0.1ms)  SELECT  "messages".* FROM "messages" WHERE "messages"."id" = ? LIMIT 1  [["id", 1]]
"-----------------"
  Comment Load (0.1ms)  SELECT "comments".* FROM "comments" WHERE "comments"."commentable_id" = ? AND "comments"."commentable_type" = ?  [["commentable_id", 1], ["commentable_type", "Message"]]
#<ActiveRecord::Associations::CollectionProxy []>
   (0.1ms)  SELECT COUNT(*) FROM "comments" WHERE "comments"."commentable_id" = ? AND "comments"."commentable_type" = ?  [["commentable_id", 1], ["commentable_type", "Message"]]
0
  CACHE (0.0ms)  SELECT  "messages".* FROM "messages" WHERE "messages"."id" = ? LIMIT 1  [["id", "1"]]
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 2]]
  Rendered comments/_form.html.erb (1.0ms)
  Rendered messages/show.html.erb within layouts/application (1.9ms)
Completed 200 OK in 40ms (Views: 34.5ms | ActiveRecord: 0.4ms)

1 个答案:

答案 0 :(得分:2)

为什么在show动作中输出

您应该只定义@instance_variables并将它们传递给视图进行渲染:

#config/routes.rb
resources :users do
   resources :comments, only: [:show, :create]
end

#app/controllers/messages_controller.rb
class MessagesController < ApplicationController
    def show
        @message = Message.find params[:id]
    end
end

#app/views/messages/show.html.erb
<%= @message.title %>
<%= render @message.comments if @message.comments.any? %>

#app/views/messages/_comment.html.erb
<% comment.title %>
<% comment.body %>

这将输出顶级评论。

如果你想要嵌套评论,我高度建议使用acts_as_tree。这使您可以访问“子”对象(在表中使用parent列设置),这样您就可以执行以下操作:

<%= render @message.comments if @message.comments.any? %>

#app/views/messages/_comment.html.erb
<%= render comment.children if comment.children.any? %>

注释

<强> 1。 VARS

当您运行循环(<% @message.comments.each do |comment| %>)时,您需要使用块中的local variable

@message.comments.each do |comment|
   comment.title
   comment.body
end

您目前正在使用 @comment.title - 应为comment.title

-

<强> 2。评论创建

您可以通过messages#show视图中嵌入的表单创建评论:

#app/views/messages/show.html.erb
<%= render "comments/new" %>

您必须确保设置@comment变量:

#app/controllers/messages_controller.rb
class MessagesController < ApplicationController
   def show
      @message = Message.find params[:id]
      @comment = Comment.new
   end
end 

#app/controllers/comments_controller.rb
class CommentsController < ApplicationController
   def create
      @comment = Comment.new comment_params
   end

   private

   def comment_params
      params.require(:comment).permit(:title, :body)
   end
end

你当然已经这样做了 - 我认为它可以清理很多。

-

第3。迁移

最后,您在表中使用了多态关联。在这种情况下不应该使用它;你应该有一个标准foreign_key如下:

enter image description here

create_table "comments", force: :cascade do |t|
  t.integer  "message_id"
  t.string   "title"
  t.text     "body"
  t.string   "subject"
  t.integer  "user_id",          null: false
  t.integer  "parent_id"
  t.datetime "created_at"
  t.datetime "updated_at"
end

这将允许以下内容:

#app/models/message.rb
class Message < ActiveRecord::Base
   has_many :comments
end

#app/models/comment.rb
class Comment < ActiveRecord::Base
   belongs_to :message
   belongs_to :user

   acts_as_tree
end