【问题标题】:Passing parent_id to a comment将 parent_id 传递给评论
【发布时间】: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


    【解决方案1】:

    祖先

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

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

    这使我们可以灵活地创建更健壮的嵌套结构(我稍后会详细介绍):

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

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

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

    【讨论】:

    • 你知道什么。我之前尝试过使用祖先,但你的指南有点不同,所以我又试了一次。像魅力一样工作!谢谢。
    • 哇,谢谢你的夸奖!喜欢它起作用的事实!你也想要我们制作的“嵌套下拉”助手吗?
    • 其实我会喜欢的。我正要从头开始编写代码:)
    • +1。在我们可以使用像 Ancestry 这样的大多数罐头解决方案时,我们不应该重新发明轮子。
    • 是的,但你必须记住,找到最新的实际上是相当困难的,尤其是对于“嵌套下拉”和“嵌套部分”:)
    【解决方案2】:

    为 cmets 制作如下形式:

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

    【讨论】:

    • 这不起作用。我收到一个错误 undefined method comments_path' (because my Posts have many comments). If I do forms_for[@posts,@comment] I get an error saying undefined method merge' for 3364:Fixnum for the f.hidden_​​field
    • 实际上你可以为这个表单设置post url,你能写出cmets的post/put url来进行新/编辑操作吗?
    • 了解form_for
    猜你喜欢
    • 2016-11-30
    • 2015-05-18
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 2013-04-06
    相关资源
    最近更新 更多