Rails在破坏行动后重定向

时间:2018-03-04 00:40:32

标签: ruby-on-rails ruby-on-rails-5

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

未定义的方法`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

1 个答案:

答案 0 :(得分:2)

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

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

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