Rails 发表评论后重定向到 post

Rails redirect to the post after a comment is made

我的门票展示页面上有一个评论表。我可以填写,但是提交评论时,它会进入评论显示页面。我需要这个才能返回显示的票证。

我现在有这个代码:

comment_controller.rb

def create
  @comment = Comment.new(comment_params)

  respond_to do |format|
    if @comment.save
      format.html { redirect_to @comment, notice: 'Comment was successfully created.' }
      format.json { render :show, status: :created, location: @comment }
    else
      format.html { render :new }
      format.json { render json: @comment.errors, status: :unprocessable_entity }
    end
  end
end

和 destroy 方法类似的事情

def destroy
  @comment.destroy
  respond_to do |format|
    format.html { redirect_to comments_path, notice: 'Comment was successfully destroyed.' }
    format.json { head :no_content }
  end
end

我不确定如何让它记住它在哪张票上,以便它重定向到。

我已经与 ticket.rb 和 comments.rb

的模特建立了联系

你可以直接替换

redirect_to comments_path

redirect_to :back
# or 
redirect_to(:back)

评论后returns到上一页

如果您使用 rails 5,请使用

redirect_back(fallback_location: root_path)

改为