【发布时间】:2014-08-25 09:57:35
【问题描述】:
在我的应用程序中,我有带有 cmets 的帖子。现在我还想将 cmets 添加到这些 cmets(嵌套评论)。
我目前是这样做的,但它不会将我的 parent_id 写入数据库:
我的 _comment.html.erb 部分中的评论链接:
<%= link_to "Comment", new_post_comment_path(comment.post, parent_id: comment.id) %>
我的 cmets/new.html.erb:
<%= form_for [@post, Comment.new] do |f| %>
<%= f.hidden_field :parent_id %>
<%= f.text_area :body %>
<%= f.submit %>
<% end %>
我的 cmets_controller.rb:
def new
@post=Post.find(params[:post_id])
@comment = Comment.new(parent_id: params[:parent_id])
end
def create
@post = Post.find(params[:post_id])
@comment=@post.comments.build(comment_params)
@comment.user=current_user
if @comment.save
redirect_to :back
end
private
def comment_params
params.require(:comment).permit(:body, :parent_id)
end
被执行的查询:
INSERT INTO "comments" ("body", "created_at", "post_id", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4