views:

95

answers:

2

I am using rails beta 3 and I have a erb page named index.html.erb for discussions controller. In that page, I have a link as following:

<%= link_to 'Delete', {:action=>'destroy', :id=>@discussion}, :confirm=>"Are you sure", :method=>'post' %>

Which is supposed to generate a link to delete a discussion, however, the generated html is

<a href="/discussions/1" data-confirm="Are you sure" data-method="post" rel="nofollow">Delete</a>

Which always routes to the show action. I think the href should be /discussions/destroy/1. But for some reason it is not.

Any ideas? Thanks in advance.

A: 

When looking at the routing guide the delete link should be in the form as it is in your example. Except that the method should be DELETE instead of POST. But this might be a compatibly issue/fix since the DELETE method is 'less' supported compared to GET and POST.

The show action is activated with the GET method on your link, which should clearly not the case looking at your link.

So are you sure your routes are setup properly? Something like:

map.resources :discussions

Edit: I just saw that your link_to code has an error in it, :method should be set to :delete:

<%= link_to 'Delete', {:action=>'destroy', :id=>@discussion}, :confirm=>"Are you sure", :method=>:delete %>

Furthermore you could use something like discussion_path(@discussion_item) for the path (URL) to be generated. This is more clear than manually create the path. FOr this to work you need the route setup as soon above and a @discussion_item variable containing the information of the current (shown) page.

So your link_to line becomes like this:

<%= link_to 'Delete', discussion_path(@discussion_item), :confirm=>"Are you sure", :method=>:delete %>
Veger
A: 

That is the correct href, but the method should be :delete. You also need to make sure you're properly set up with the new unobtrusive javascript way of doing things in rails 3. You might need to add the csrf_meta_tag helper, as described in this link:

http://www.themodestrubyist.com/2010/02/24/rails-3-ujs-and-csrf-meta-tags/

mckeed
That works! Thanks a lot, mckeed!
Wei