6
votes

I'm using Rails 3, i have a link_to with method :delete, but when i click this link it goes to the show action instead the destroy one.

my question is how to solve this problem?

thanks in advance.

code: <%= link_to 'Delete', list_path(@list.id), :method => :delete, :confirm => 'Are you sure?' %>

2
Probably want to take a look at similar questions like this one: stackoverflow.com/questions/3774925/… - Matt Glover

2 Answers

10
votes

You don't have the jquery and jquery_ujs libraries included by your application.js file, or you're not even including your application.js file in app/views/layouts/application.html.erb. This is typically done with this line:

<%= javascript_include_tag "application" %>
0
votes

Hi you can try this:

application.html.erb:

<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js", "jquery.rails.js" %>

and your link code should be like this:

<%= link_to "<i class='icon-trash'></i> Delete".html_safe, item, :confirm => "Are you sure you want to delete item: " + item.name + "?" ,:method => :delete%>

and your controller should have like this:

def destroy
 @item = Item.find(params[:id])
 if @item.destroy
  redirect_to items_path, :notice => "Successfully deleted an item."
 else 
  redirect_to items_path, :notice => "Failed to delete an item."
 end
end