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 %>
destroyrequires the request be made by the HTTPDELETEmethod. Just visiting this route in the browser will create aGETrequest and will probably raise a no route matches error. Most browsers do not support trueDELETErequests so rails falsifies this for you by adding a_methodparam and then handles the request appropriately in it's internals. As a side note you did not post yourbillsroutes but ratherpostslink_to 'Destroy', postthis seems to be an on going issue are you sure you are posting the actual source?