Ruby on Rails button is not working as it should (help?)

In my project the "Destroy" button works like "Show" button. The project was made using scaffolds. I have no idea what is wrong, can you help me?

Here are my controllers files

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

Upvotes: 0

Views: 50

Answers (2)

Mandeep
Mandeep

Reputation: 9173

If you'll do rake routes in your console you'll see that the url for both show and destroy are exactly same, only the verb part is different. If it'll be a get request it'll take you to show action and if it's using a delete verb then it'll take you to destroy action so this can be caused by two things:

a. As @wally pointed out make sure you are using delete method in your link:

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

b. Delete method requires jquery_ujs in your app. Rails ships with jquery_ujs by default but make sure you have it in your app

Upvotes: 0

Wally
Wally

Reputation: 61

Make sure you have set method: :delete on button link

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

Upvotes: 1

Related Questions