为什么数据库回滚时会创建新评论? Ruby on Rails

时间:2018-02-04 22:26:23

标签: ruby-on-rails ruby rubygems

我有一个问题,我过去一周一直试图解决这个问题。我正在尝试使用closure_tree gem创建一个嵌套的注释系统。我终于想出了如何将parent_id作为params的一部分传递,现在整个哈希在服务器日志中正确填充,但每次我尝试保存回复时,我的日志会在保存注释之前显示回滚。所有评论都属于wad。

以下是我的评论控制器中的相关代码:

class CommentsController < ApplicationController
    before_action :find_wad
    before_action :find_comment, only: [:destroy, :edit, :update, :comment_owner, :comment_params]
    before_action :comment_owner, only: [:destroy, :edit, :update]

    def create
        if params[:comment][:parent_id].to_i > 0
            parent = Comment.find_by(params[:comment].delete(:parent_id))
            @comment = parent.children.build(comment_params)
            parent_id = parent.id

        else
            @comment = @wad.comments.build(comment_params)
            parent_id = @comment.id
        end
            @comment.user_id = current_user.id
            @comment.save

        if @comment.save
            flash[:success] = 'Your comment was successfully added!'
            redirect_to wad_path(@wad)
        else
            render 'new'
        end
    end


    def new
        @comment = Comment.new(parent_id: params[:parent_id])
    end

private

    def find_wad
        @wad = Wad.find(params[:wad_id])
    end

    def find_comment
        @comment = @wad.comments.find_by(params[:id])
    end

    def comment_params
        params.require(:comment).permit(:content, :wad_id, :user_id, :parent_id)
    end


    def comment_owner
        unless current_user.id == @comment.user_id
            flash[:notice] = "Action Restricted"
            redirect_to @wad

        end
    end
end

以下是我在评论视图中的_reply partial中使用的回复表单:

<%= form_for([@wad, @comment]) do |f| %>
<%= f.hidden_field :parent_id %>
<%= f.text_area :content %>
<%= f.submit %>

<% end %>

以下是我尝试输入回复评论时在我的服务器日志中发生的情况:

Started POST "/wads/1/comments" for 127.0.0.1 at 2018-02-04 14:20:04 -0800
Processing by CommentsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"LYk07p6h3+RJ6kfx+rixaGJ0a56XPt29v7mFFbCQ+H+Gr5PjzzkrWOWfShCuKwFEIShwEz8Om7IsLjGI3hN0vw==", "comment"=>{"parent_id"=>"136", "content"=>"elmle"}, "commit"=>"Create Comment", "wad_id"=>"1"}
  Wad Load (0.2ms)  SELECT  "wads".* FROM "wads" WHERE "wads"."id" = ? ORDER BY "wads"."created_at" DESC LIMIT ?  [["id", 1], ["LIMIT", 1]]
  Comment Load (0.2ms)  SELECT  "comments".* FROM "comments" WHERE (136) LIMIT ?  [["LIMIT", 1]]
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
   (0.1ms)  begin transaction
  CACHE User Load (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
   (0.1ms)  rollback transaction
   (0.1ms)  begin transaction
   (0.0ms)  rollback transaction

我的任何模型都没有attr_accessor - 只是正确定义的has_many和belongs_to关系。有没有人知道为什么会这样?

Wad Model:

class Wad < ApplicationRecord
     acts_as_votable
      belongs_to :user
      has_many :comments, dependent: :destroy
      default_scope -> { order(created_at: :desc) }
      validates :user_id, presence: true
      validates :category, presence: true
      validates :long_form, presence: true, length: { maximum: 1000 }
      validates :short_form, presence: true, length: { maximum: 140 }
      validates :problem_state, presence: true, length: { maximum: 50 }
    end

评论模型:

class Comment < ApplicationRecord
  acts_as_tree order: 'created_at DESC'
  belongs_to :wad
  belongs_to :user
end

用户模型的相关部分:

class User < ApplicationRecord
  acts_as_voter
  has_many :wads, dependent: :destroy
  has_many :comments

2 个答案:

答案 0 :(得分:1)

据我所知,日志,参数是:

{"comment"=>{"parent_id"=>"136", "content"=>"elmle"}, "commit"=>"Create Comment", "wad_id"=>"1"}

在这种情况下,我们将会遇到这种情况:

if params[:comment][:parent_id].to_i > 0
  parent = Comment.find_by(params[:comment].delete(:parent_id))
  @comment = parent.children.build(comment_params)
  parent_id = parent.id

params["comment"]创建仅包含parent_idcontent的新评论对象。 因此,在您的模型中可能没有wad_id所需的belongs_to密钥的情况下创建新对象

答案 1 :(得分:0)

使用时

Comment.find_by(params[:comment].delete(:parent_id))

你从params [:comment]的哈希中删除你的:parent_id,你需要在comment_params中从permit中删除。尝试改变你的方法,它应该工作:

def comment_params
    params.require(:comment).permit(:content, :wad_id, :user_id)
end

修改 另外我认为你需要改变

中的第一个命令
parent = Comment.find_by_id(params[:comment].delete(:parent_id))

 parent = Comment.find(params[:comment].delete(:parent_id))