3

So far I've managed to come up with this piece of code:

  1   <% @projects.each do |p| %>
  2       <%= link_to p.name, project_path(p) %>
  3       <%= link_to "edit", edit_project_path(p) %>
  4       <%= link_to "delete", project_path(p), :method => :delete %>
  5   <% end %>

When I click on 'delete' link it takes me to destroy.html.erb, not delete.html.erb. It also communicates with controllers 'destroy' action, not with 'delete' action. In addition, the URL looks like this /projects/1 (no /delete or /destroy). What is wrong here? Is this the best way to create delete link?

2
  • Please, can you add your routes configuration to the question? Commented Mar 14, 2012 at 22:44
  • I only have 2 lines. resources :projects (and) resources :users Commented Mar 14, 2012 at 22:53

2 Answers 2

1

This is the correct behavior of Rails. The :delete method links to the destroy action and adds nothing to the URL -- the proper action is connected by the HTTP verb. For more information on the Rails routing structure and defaults, check out the Rails routing guide.

Sign up to request clarification or add additional context in comments.

2 Comments

ok, thanks. so can you please tell how a delete link should look like?
That syntax is correct. Go read the guide I linked for instructions on how to implement it.
1

Veraticus is absolutely right. Rails is doing what it is supposed to be doing.

However, if you actually want to go to a delete page (instead of having a javascript confirmation) than you can certainly do that.

For example, if I had a 'Pages' controller, than I could have both a 'delete' and a 'destroy' action like this:

  def delete
    @page = Page.find(params[:id])
    render 'delete'
  end

  def destroy
    page = Page.find(params[:id])        
    page.destroy
    flash[:notice] = "Page successfully destroyed"
    redirect_to(:action => 'index')
  end

And inside your Page index view, you would need a link that passes the page.id over to the delete method:

<%= link_to "delete",{ :action => 'delete', :id => @page.id } %>

And, of course, you would need to create a 'delete' view in your views folder in order to give it something to render.

Inside the 'delete.html.erb' file, you would need a form that sends a destroy action:

<%= form_for(:page, :url => {:action => 'destroy', :id => @page.id }) do |f| %>
   < All your form components and submit tag will go here >
<% end %>

7 Comments

that is exactly what I wanted. but this returns routing error >>> No route matches [GET] "/assets"
Without seeing your routes, controllers, and views it will be hard to troubleshoot. Could you put examples of your code in a gist and link them to your question?
I have put examples in the gist here gist.github.com/2040730 . thank you for helping me. if you need something else to solve this, let me know.
Just out of curiosity, do you have this at the bottom of your routes file: match ':controller(/:action(/:id(.:format)))'
yes! i uncommented it and it shows the form and everything, but now returns this error >> Unknown action The action '6' could not be found for ProjectsController
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.