6

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?' %>

  • Probably want to take a look at similar questions like this one: http://stackoverflow.com/questions/3774925/delete-link-sends-get-instead-of-delete-in-rails-3-view – Matt Glover Jul 04 '12 at 21:06

2 Answers2

10

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" %>
Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261
0

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
AllenC
  • 2,754
  • 1
  • 41
  • 74