0

i have created a new action using scaffold but when i click on the delete button, it displays the show page instead, My controller is

class CattleClientsController < ApplicationController
  before_action :set_cattle_client, only: [:show, :edit, :update, :destroy]

  # GET /cattle_clients
  # GET /cattle_clients.json
  def index
    @cattle_clients = CattleClient.all
  end

  # GET /cattle_clients/1
  # GET /cattle_clients/1.json
  def show
  end

  # GET /cattle_clients/new
  def new
    @cattle_client = CattleClient.new
  end

  # GET /cattle_clients/1/edit
  def edit
  end

  # POST /cattle_clients
  # POST /cattle_clients.json
  def create
    @cattle_client = CattleClient.new(cattle_client_params)

    logger.debug "New Cattle Client:                                                                       #{@cattle_client.attributes.inspect}"
    logger.debug "New Cattle Client should be valid: #{@cattle_client.valid?}"

    respond_to do |format|
      if @cattle_client.save
        format.html { redirect_to @cattle_client, notice: 'Cattle client was successfully created.' }
        format.json { render :show, status: :created, location: @cattle_client }
      else
        format.html { render :new }
        format.json { render json: @cattle_client.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /cattle_clients/1
  # PATCH/PUT /cattle_clients/1.json
  def update
    respond_to do |format|
      if @cattle_client.update(cattle_client_params)
        format.html { redirect_to @cattle_client, notice: 'Cattle client was successfully updated.' }
        format.json { render :show, status: :ok, location: @cattle_client }
      else
        format.html { render :edit }
        format.json { render json: @cattle_client.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /cattle_clients/1
  # DELETE /cattle_clients/1.json
  def destroy
    @cattle_client.destroy
    respond_to do |format|
      format.html { redirect_to cattle_clients_url, notice: 'Cattle  client was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_cattle_client
      @cattle_client = CattleClient.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def cattle_client_params
      params.require(:cattle_client).permit(:client_name, :milk, :date)
    end
end

And my view page for the index is

<p id="notice"><%= notice %></p>

<h1 class="title">Listing Cattle Clients</h1>

<div class"btn-group menu2" align = right>
<%= link_to raw("<span class=''></span> Cattle"), cattles_path, :class=>"btn btn-default" %>
<%= link_to "Home", root_path, :class=>"btn btn-default" unless current_page?(root_url) %>
</div>

<table>
  <thead>
    <tr>
      <th>Client name</th>
      <th>Milk</th>
      <th>Date</th>
      <th colspan="3"></th>
</tr>
  </thead>

  <tbody>
    <% @cattle_clients.each do |cattle_client| %>
      <tr>
        <td><%= cattle_client.client_name %></td>
        <td><%= cattle_client.milk %></td>
        <td><%= cattle_client.date %></td>
        <td><%= link_to 'Show', cattle_client %></td>
        <td><%= link_to 'Edit', edit_cattle_client_path(cattle_client) %></td>
        <td><%= link_to 'Destroy', cattle_client, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>
      <br>
      <%= link_to 'New Cattle client', new_cattle_client_path %>

Anyone help me identify why my delete button carries out the show action instead of deleting a column in the index page?

BARBRA
  • 11
  • 1
  • 6
  • can you verify that in the server logs that the action is of type DELETE...and not GET.... ?I mean to say that is it a DELETE request or GET request ? – Milind Jul 15 '15 at 10:33
  • Your routes are not matching I suspect. Can you check rake routes and give some feedback. Also as an aside, check you have jquery-rails installed properly. – GhostRider Jul 15 '15 at 10:40
  • Ohh, when i check using rake routes, it shows that its a DELETE request – BARBRA Jul 15 '15 at 10:55
  • didnt have the jquery-rails gem, have installed it now, but no change yet – BARBRA Jul 15 '15 at 11:05
  • As Milind suggested, look at the server (in terminal/command line) and see what it says when you click your delete button. I would advise clearing it first by typing command-k (MAC) or ctrl-k (PC). Then click delete and see what route and action s occurring. – GhostRider Jul 15 '15 at 11:27
  • Started GET "/cattle_clients/10" for 127.0.0.1 at 2015-07-15 +0300 Processing by CattleClientsController#show as HTML Parameters: {"id"=>"10"} CattleClient Load (4.0ms) SELECT `cattle_clients`.* FROM `cattle_clients` WHERE `cattle_clients`.`id` = 10 LIMIT 1 Rendered cattle_clients/show.html.erb within layouts/application (14.0ms) Rendered shared/_header.html.erb (0.0ms) Completed 200 OK in 411ms (Views: 378.0ms | ActiveRecord: 4.0ms) [2015-07-15 14:39:41] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true – BARBRA Jul 15 '15 at 11:46
  • thats what i get in the server – BARBRA Jul 15 '15 at 11:47
  • Ok - I guess that's what we'd expect based upon what you have posted. From what you have given I can't see why your code isn't working. Have a look at this http://stackoverflow.com/questions/3784633/delete-destroy-is-not-working-in-rails-3-with-jquery – GhostRider Jul 15 '15 at 11:54
  • One last thing - try :method => :delete – GhostRider Jul 15 '15 at 12:01
  • Thanx @GhostRider, that link definitely answered me asap. – BARBRA Jul 15 '15 at 12:13
  • Please mark my answer if it is correct. Thanks – GhostRider Jul 15 '15 at 12:38

1 Answers1

0

Please try :method => :delete

Also be sure that jquery-rails has been properly installed and the following code is in your head tags

         <%= javascript_include_tag :defaults %>
         <%= csrf_meta_tag %>
GhostRider
  • 2,109
  • 7
  • 35
  • 53