Ruby on Rails按钮不能正常工作(帮助?)

时间:2014-09-11 14:23:06

标签: ruby-on-rails

在我的项目中," Destroy"按钮就像" Show"按钮。该项目是使用脚手架制作的。我不知道出了什么问题,你能帮助我吗?

这是我的控制器文件

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

2 个答案:

答案 0 :(得分:1)

确保您已设置方法::按钮链接上的删除

<%= link_to "Delete", post, data: { confirm: 'Are you sure?' }, method: :delete, :class => "button" %>

答案 1 :(得分:0)

如果您在控制台中执行rake路由,您会发现show和destroy的url完全相同,只有动词部分不同。 如果它是一个获取请求,它将带您显示操作,如果它使用删除动词,那么它将带您去除行动<\ n / em>所以这可能是由两件事引起的:

一个。正如@wally指出的那样,请确保您在链接中使用删除方法

 = link_to "Delete", post, data: { confirm: 'Are you sure?' }, method: :delete, class: "button"

删除方法在您的应用中需要jquery_ujs。默认情况下,Rails附带jquery_ujs,但请确保在应用中使用