将 parent_id 传递给评论 [英] Passing parent_id to a comment

查看:38
本文介绍了将 parent_id 传递给评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my app I have Posts which have comments. Now I also want to add comments to those comments (nested commenting).

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

I currently do it like this, but it does not write my parent_id to the database:

我的 _comment.html.erb 部分中的评论链接:

My comment link in my _comment.html.erb partial:

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

我的评论/new.html.erb:

My comments/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:

My 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"

推荐答案

祖先

我本来想告诉你如何根据需要分配 parent_id,但考虑到我们之前已经创建了一个评论系统,我觉得最好给你一些系统的想法;而不是细节

I was going to give you an idea as to how you could assign the parent_id as you need, but considering we've created a comments system before, I felt it better to give you some systemic ideas; rather than specifics

我们使用名为 Ancestry 的 gem 使我们能够嵌套我们的对象:

We use a gem called Ancestry to give us the ability to nest our objects:

这使我们能够灵活地创建更强大的嵌套结构(我将在一分钟内详细介绍):

This gives us the flexibility to create much more robust nesting structures (which I'll detail in a minute):

#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(我推荐的真正原因)的美妙之处在于能够创建真正的嵌套视图:


The beauty of using ancestry (the real reason for my recommendation) is the ability to then create a truly nested view:

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

To do this, you can use the partial we created:

#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 %>

--

下拉

#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 %>

这篇关于将 parent_id 传递给评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆