在我的项目中,"销毁"按钮与"显示"按钮类似。这个项目是用脚手架做的。我不知道怎么了,你能帮我吗?
这是我的控制器文件
class BloknotsController < ApplicationController
before_action :set_bloknot, only: [:show, :edit, :update, :destroy]
# GET /bloknots
# GET /bloknots.json
def index
@bloknots = Bloknot.all
end
# GET /bloknots/1
# GET /bloknots/1.json
def show
end
# GET /bloknots/new
def new
@bloknot = Bloknot.new
end
# GET /bloknots/1/edit
def edit
end
# POST /bloknots
# POST /bloknots.json
def create
@bloknot = Bloknot.new(bloknot_params)
respond_to do |format|
if @bloknot.save
format.html { redirect_to @bloknot, notice: 'Bloknot was successfully created.' }
format.json { render :show, status: :created, location: @bloknot }
else
format.html { render :new }
format.json { render json: @bloknot.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /bloknots/1
# PATCH/PUT /bloknots/1.json
def update
respond_to do |format|
if @bloknot.update(bloknot_params)
format.html { redirect_to @bloknot, notice: 'Bloknot was successfully updated.' }
format.json { render :show, status: :ok, location: @bloknot }
else
format.html { render :edit }
format.json { render json: @bloknot.errors, status: :unprocessable_entity }
end
end
end
# DELETE /bloknots/1
# DELETE /bloknots/1.json
def destroy
@bloknot.destroy
respond_to do |format|
format.html { redirect_to bloknots_url, notice: 'Bloknot was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_bloknot
@bloknot = Bloknot.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def bloknot_params
params.require(:bloknot).permit(:NameOfBkoknot)
end
end
确保您已设置方法::删除按钮链接
<%= link_to "Delete", post, data: { confirm: 'Are you sure?' }, method: :delete, :class => "button" %>
如果您在控制台中执行rake路由,您将看到show和destroy的url完全相同,只是动词部分不同 如果是获取请求,则需要您显示操作,如果使用删除动词,则需要销毁操作,因此这可能是由两件事引起的:
a。正如@wally
所指出的,请确保您在链接中使用删除方法:
= link_to "Delete", post, data: { confirm: 'Are you sure?' }, method: :delete, class: "button"
bDelete方法需要应用程序中的jquery_ujs
。Rails默认附带jquery_ujs,但请确保您的应用程序中有它