Rails在破坏行动后重定向

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Rails在破坏行动后重定向相关的知识,希望对你有一定的参考价值。

我有一个名为Group的模型,其中包含has_many帖子和每个帖子has_many评论。我可以导航到一个组,创建一个帖子,然后在该帖子下创建一个评论。我的问题是,当我尝试删除评论时,我收到以下关于重定向的错误(评论被删除,重定向不会发生)。

nil的未定义方法`id':NilClass

这是我不喜欢的destry操作中的行,这很奇怪,因为我的更新操作具有相同的重定向,并且工作正常。

  format.html { redirect_to group_post_url(group_id: @group.id, id: @post), notice: 'Comment was successfully destroyed.' }

我尝试将[:destroy]添加到set_post before_action,认为它可能需要但没有运气。

这是我的一些路线

        group_posts GET    /groups/:group_id/posts(.:format)                            posts#index
                    POST   /groups/:group_id/posts(.:format)                            posts#create
     new_group_post GET    /groups/:group_id/posts/new(.:format)                        posts#new
    edit_group_post GET    /groups/:group_id/posts/:id/edit(.:format)                   posts#edit
         group_post GET    /groups/:group_id/posts/:id(.:format)                        posts#show
                    PATCH  /groups/:group_id/posts/:id(.:format)                        posts#update
                    PUT    /groups/:group_id/posts/:id(.:format)                        posts#update
                    DELETE /groups/:group_id/posts/:id(.:format)                        posts#destroy
             groups GET    /groups(.:format)                                            groups#index
                    POST   /groups(.:format)                                            groups#create

这是我的comments_controller.rb

class CommentsController < ApplicationController

  before_action :set_group, only: [:index, :show, :new, :edit, :create, :update]
  before_action :set_post, only: [:index, :show, :new, :edit, :create, :update]
  before_action :set_comment, only: [:show, :edit, :update, :destroy]

  # GET /posts/:post_id/comments
  def index
    @comments = @post.comments.order(created_at: :desc)
  end

  def show
  end

  # GET /posts/:post_id/comments/new
  def new
    @comment = @post.comments.new
  end

  # # GET /posts/:post_id/comments/edit
  # def edit
  # end

  # POST /posts/:post_id/comments
  def create
    # inserts current_user into the comments foriegn key for user_id.
    @comment = @post.comments.new(comment_params.merge(user_id: current_user.id))

    respond_to do |format|
      if @comment.save
        format.html { redirect_to group_post_url(group_id: @group.id, id: @post), 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

  # PATCH/PUT /posts/:post_id/comments/:id
  def update
    respond_to do |format|
      if @post.comments.update(comment_params)
        format.html { redirect_to group_post_url(group_id: @group.id, id: @post), notice: 'Comment was successfully updated.' }
        format.json { render :show, status: :ok, location: @comment }
      else
        format.html { render :edit }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/:post_id/comments/:id
  def destroy
    @comment.destroy
    respond_to do |format|
      format.html { redirect_to group_post_url(group_id: @group.id, id: @post), notice: 'Comment was successfully destroyed.' }
      format.json { head :no_content }
    end
  end


  ###################
  # private methods
  ###################
  private
    # Use callbacks to share common setup or constraints between actions.
    def set_comment
      @comment = Comment.find(params[:id])
    end

    def set_post
      @post = Post.find(params[:post_id])
    end

    def set_group
      @group = Group.find(params[:group_id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def comment_params
      params.require(:comment).permit(:content, :post_id, :user_id)
    end
end
答案

如果你想引用@group实例变量,你应该将:destroy添加到before_action set_group回调中:

before_action :set_group, only: [:index, :show, :new, :edit, :create, :update, :destroy]

您还应该将:destroy添加到before_action set_post回调中

以上是关于Rails在破坏行动后重定向的主要内容,如果未能解决你的问题,请参考以下文章

在生产模式中无意中破坏了Rails Devise会话

播放后重定向html5视频

KeyCloak 在身份验证代码流错误后重定向回身份提供者

在 Laravel 中登录多个角色后重定向

Django Forms - 保存后重定向

用户登录后重定向到要访问页面