将parent_id传递给评论

2023-12-01

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

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

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

<%= 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])
    @[email protected](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"

Ancestry

我打算给你一个关于如何分配的想法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 %>

--

Dropdown

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 %>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将parent_id传递给评论 的相关文章

随机推荐