Jason M
Jason M

Reputation: 11

Rails 4.1 link_to delete gives incorrect path

For some reason I am unable to get the correct path for my destroy operation of a very simple model. Are my expectations incorrect?

My routes.rb includes:

resources :designs

And my view contains:

<% @designs.each do |design| %>
<%= link_to "Delete", design, :method => :delete %>
<% end %>

Which results in the HTML:

<a data-method="delete" href="/designs.49" rel="nofollow">Delete</a>

Which of course errors on

"No route for [DELETE] for /designs.49"

When I was expecting the rendered HTML to be:

<a data-method="delete" href="/designs/49" rel="nofollow">Delete</a>

Especially considering rake routes shows me:

 DELETE /designs/:id(.:format)                      designs#destroy

My workaround is to replace: link_to "Delete", design... with: link_to "Delete", "/designs/#{design.id}"... (which works fine), but surely I am overlooking something basic, as no one should have to waste this much time to figure out the absolute most fundamental baseline case for a destroy operation.

Upvotes: 0

Views: 498

Answers (2)

Chris Peters
Chris Peters

Reputation: 18090

Your code in the view could read like this using a _path helper:

<% @designs.each do |design| %>
  <%= link_to "Delete", design_path(design), :method => :delete %>
<% end %>

But I guess I can see what you're trying to accomplish. To get the show action, you should be able to do this:

<% @designs.each do |design| %>
  <%= link_to "Show", design %>
<% end %>

I wonder if this is a bug in Rails? What happens if you do this?

<% @designs.each do |design| %>
  <%= link_to "Delete", url_for(design), :method => :delete %>
<% end %>

Upvotes: 1

Jorge de los Santos
Jorge de los Santos

Reputation: 4643

Try to replacing this in the helper tag.

<%= link_to "Delete", design_path, :method => :delete %>

Upvotes: 0

Related Questions