[英]Rails redirect after destroy action
我有一個名為Group的模型,該模型具有has_many個帖子,每個帖子都有has_many個注釋。 我可以導航到一個組,創建一個帖子,然后在該帖子下創建評論。 我的問題是,當我嘗試刪除評論時,收到以下有關重定向的錯誤(評論被刪除,重定向不會發生)。
nil:NilClass的未定義方法'id'
這是我不喜歡的嘗試操作中的這一行,這很奇怪,因為我的更新操作具有相同的重定向並且可以正常工作。
format.html { redirect_to group_post_url(group_id: @group.id, id: @post), notice: 'Comment was successfully destroyed.' }
我嘗試在set_post before_action上添加[:destroy],以為可能需要這樣做,但沒有運氣。
這是我的一些路線
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
這是我的comment_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
回調中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.