【发布时间】: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