0

I am using Rails and have a controller for Bills in which there is a destroy method. In the browser, when I use the delete url e.g. localhost/delete/2 (2 in this case is the ID of the relevant bill), the bill never gets deleted. Any ideas on what I could be doing wrong (or if I should additional configs here). The routes is copied below ass well.

bills_controller

  def destroy
    @bill = Bill.find(params[:id])
    @bill.destroy
    respond_to do |format|
      format.html { redirect_to posts_url, notice: 'Bill was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

Routes:

Rails.application.routes.draw do
  resources :bills
  get 'bills/index'
end

Should I be adding a specific route for the delete action in the routes.rb file? I have the following on my HTML page:

  <td><a data-confirm="Are you sure?" rel="nofollow" data-method="delete" href="/bills/1">Destroy</a></td>

delete & index portion of the controller:

 def index
    @bills = Bill.all
  end

  # DELETE /bills/1
  # DELETE /bills/1.json
  def destroy
    @bill = Bill.find(params[:id])
    @bill.destroy
    respond_to do |format|
      format.html { redirect_to bills_url, notice: 'Bill was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

index erb file

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

<h1>Listing Bills</h1>

<table>
  <thead>
    <tr>
      <th>Title</th>
      <th>Body</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @bills.each do |bill| %>
      <tr>
        <td><%= bill.title %></td>
        <td><%= bill.body %></td>
        <td><%= link_to 'Show', bill %></td>
        <td><%= link_to 'Edit', edit_bill_path(bill) %></td>
        <td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Post', new_post_path %>
3
  • 2
    routing to destroy requires the request be made by the HTTP DELETE method. Just visiting this route in the browser will create a GET request and will probably raise a no route matches error. Most browsers do not support true DELETE requests so rails falsifies this for you by adding a _method param and then handles the request appropriately in it's internals. As a side note you did not post your bills routes but rather posts Commented Sep 10, 2015 at 19:17
  • thank you for pointing it out - what do I need to change to ensure that the delete gets called? Commented Sep 10, 2015 at 19:22
  • you have link_to 'Destroy', post this seems to be an on going issue are you sure you are posting the actual source? Commented Sep 10, 2015 at 20:24

2 Answers 2

1

Your browser will use an HTTP action of GET. You need it to be DELETE. You can install a plugin for your browser that will let you make rest calls, and set the action, but I think it's easier to user curl:

curl -X DELETE localhost/bills/2

You might need to adjust the hostname in the URL, but you get the idea.

Sign up to request clarification or add additional context in comments.

22 Comments

I have a link in the URL for delete ( <td><a data-confirm="Are you sure?" rel="nofollow" data-method="delete" href="/bills/1">Destroy</a></td>)
Yes, that should work. But just visiting that URL with the browser will not.
But you won't want to hard-code the href. Use a path helper, and pass the object to be deleted.
Like this: <%= link_to "Delete", bill_path(@bill), method: :delete, data: { confirm: "Are you sure?" }%>
@Daiku by the way link_to can infer the singular path bill_path if just the object is passed. Just some of that fun rails automagic
|
1

Issue was that my rails app vendor javascript directory was empty and not pulling in the javascript files required to enable the delete call.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.