将parent_id传递给评论

时间:2014-08-25 09:57:35

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

在我的应用中,我有帖子有评论。现在我还想为这些评论添加评论(嵌套评论)。

我目前这样做,但它不会将我的parent_id写入数据库:

我在_comment.html.erb partial中的评论链接:

<%= link_to "Comment", new_post_comment_path(comment.post, parent_id: comment.id) %>

我的评论/ new.html.erb:

<%= form_for [@post, Comment.new] do |f| %>
    <%= f.hidden_field :parent_id %>
    <%= f.text_area :body %>
    <%= f.submit %>
<% end %>

我的comments_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"

2 个答案:

答案 0 :(得分:2)

<强>祖先

我打算让您了解如何根据需要分配parent_id,但考虑到我们之前已经创建了评论系统,我感觉更好给你一些系统的想法;而不是具体细节

我们使用名为Ancestry的宝石来为我们提供嵌套对象的能力:

enter image description here

这使我们可以灵活地创建更强大的嵌套结构(我将在一分钟内详述):

#config/routes.rb
resources :posts do
   resources :comments, only: [:new, :create] do
      get :reply #-> domain.com/posts/:post_id/comments/:id/reply
   end
end

#app/controllers/comments_controller.rb
Class CommentsController < ApplicationController
   def new
      @post = Post.find params[:post_id]
      @comment = Comment.new
   end

   def reply
      @post = Post.find params[:post_id]
      @comment = Comment.new
      @parent = params[:id]

      render :new
   end

   def create
      @post = Post.find params[:post_id]
      @comment = @post.comments.new comment_params
      @comment.save
   end

   private

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


#app/views/comments/new.html.erb
<%= form_for [@post, @comment] do |f| %>
   <%= f.text_field :body %>
   <%= f.hidden_field :ancestry, value: @parent if @parent.present? %>
   <%= f.submit %>
<% end %>

使用ancestry(我推荐的真正原因)的美妙之处在于能够创建真正嵌套的视图:

enter image description here

为此,您可以使用我们创建的部分:

#app/comments/index.html.erb
<%= render partial: "category", collection: @comments, as: :collection %>

#app/comments/_comment.html.erb
<% collection.arrange.each do |comment, sub_item| %>
        <li>
            <%= link_to comment.title, comment_path(comment) %>
            <% if comment.has_children? %>
                <%= render partial: "comment", locals: { collection: comment.children } %>
            <% end %>
        </li>
<% end %>

-

<强>下拉

enter image description here

#app/helpers/application_helper.rb
def nested_dropdown(items)
        result = []
        items.map do |item, sub_items|
            result << [('- ' * item.depth) + item.name, item.id]
            result += nested_dropdown(sub_items) unless sub_items.blank?
        end
    result
end


#app/views/posts/new.html.erb
<%= form_for @post do |f| %>
   <%= f.select(:category_ids, nested_dropdown(Category.all.arrange), prompt: "Category", selected: @category ) %>
<% end %>

答案 1 :(得分:0)

按以下方式填写评论表单:

<%= form_for @comment do |f| %>
  <%= f.hidden_field :parent_id, @post.id %>
  <%= f.text_area :body %>
  <%= f.submit %>
<% end %>