Leonel Leonardis
Leonel Leonardis

Reputation: 61

Delete link goes to show method

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

Upvotes: 6

Views: 3546

Answers (2)

AllenC
AllenC

Reputation: 2754

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

Upvotes: 0

Ryan Bigg
Ryan Bigg

Reputation: 107728

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" %>

Upvotes: 10

Related Questions