【发布时间】:2013-09-19 17:31:45
【问题描述】:
所以我将 acts_as_commentable_with_threading 用于类似于 Reddit 的评论系统。
所以在一个项目的显示页面上,我有一个 form_for,它有 (items/show.html.haml):
- if user_signed_in?
%h5 Have something to say?
= form_for([@item, @new_comment], remote: true) do |f|
.form-group
= f.text_area :body, class: "form-control", rows: "3"
= f.hidden_field :user_id, value: current_user.id
.form-group
= f.submit "Submit", class: "btn btn-sm"
使用控制器 (items_controller.rb):
def show
@item = Item.find(params[:id])
@comments = @item.comment_threads
if user_signed_in?
@new_comment = Comment.build_from(@item, current_user.id, "")
end
end
然后会有一个 create.js.erb 将新创建的评论附加到页面上
$('#comments').append("<%= escape_javascript(render partial: 'comment', locals: { comment: @comment } ) %>");
if ("<%= @comment.body %>") {
$("#comment_body").val('')
}
这本身就有效。我渲染每个评论,然后在渲染每个评论的部分内部,如果他们有任何孩子,我也会渲染孩子 cmets。
如……:
- if !@comments.empty?
= render partial: 'comments/comment', collection: @item.root_comments, as: :comment
但是,每个评论都可以有回复,并且这些回复可以有自己的回复(再次,如 Reddit)。因此,当我尝试对孩子的回复做同样的事情时,它会给我一个 500 错误。
= form_for([@item, @new_comment], remote: true, html: { class: "comment-reply", id: "replyto_#{comment.id}" }) do |f|
.col-md-5
.form-group
= f.text_area :body, class: "form-control", rows: "3"
= f.hidden_field :user_id, value: current_user.id
= f.hidden_field :parent_id, value: comment.id
.form-group
= f.submit "Submit", class: "btn btn-sm"
%div{style: "clear:both;"}
所以我的问题是,我如何为子 cmets 创建一个 form_for,然后(可能)创建一个新的 js.erb,这样它就不会“追加”,而是重新呈现父评论(这样会依次渲染新创建的子评论)。
我想我可能需要重新创建一个,比如 create_child,但是 form_for 会变成什么?
【问题讨论】:
标签: javascript jquery ruby-on-rails acts-as-commentable