【问题标题】:How to display all comments?如何显示所有评论?
【发布时间】:2016-01-22 22:14:35
【问题描述】:

请帮我显示某个线程的所有 cmets。

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

例如,我创建脚手架“消息”。我尝试为某些消息单元制作 cmets 线程。

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

在此表中添加通过创建操作字段填充的新评论 i(和 gem)后:

title,
body,
user_id,
lft,
rgt

评论控制器:

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

添加新消息的表单工作正常,但未显示某些消息的所有 cmets。

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)

【问题讨论】:

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


    【解决方案1】:

    你为什么输出在表演动作中?

    您应该只定义 @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 %>
    

    这将输出顶级 cmets。

    如果您想要嵌套 cmets,我强烈推荐使用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.变量

    运行循环(&lt;% @message.comments.each do |comment| %&gt;)时,需要在块内使用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,如下:

    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
    

    【讨论】:

      猜你喜欢
      • 2019-04-12
      • 2017-01-13
      • 2021-10-28
      • 1970-01-01
      • 2020-02-18
      • 2013-02-23
      • 2016-11-04
      • 2013-03-14
      • 1970-01-01
      相关资源
      最近更新 更多