lulalala
lulalala

Reputation: 17981

Why are id's from nested route switched around?

I have a nested resource under my admin namespace: The admin/topic/comments_controller.rb is a resource under admin/topics_controller.rb.

  namespace :admin do
    resources :topics do
      resources :comments, :controller => "topic/comments"
    end
  end

gives me this delete route:

DELETE
/admin/topics/:topic_id/comments/:id(.:format)
admin/topic/comments#destroy

And I am creating a link to destroy comments, like the following:

# comment = @topic.comment.first
<%= link_to "Destroy", [:admin, comment], :method => :delete %>

produces the following route:

/admin/topics/165/comments/11

All seems correct, except that the two ids are swapped around. What am I doing wrong?

Upvotes: 1

Views: 183

Answers (2)

DanS
DanS

Reputation: 18463

<%= link_to 'Destroy', :action => 'destroy', :id => comment.id, :method => :delete %>

or if you use RESTFUL routes:

<%= link_to 'Destroy', delete_comment(:id => comment.id), :method => :delete %>

When working with namespaced controllers and routes, you have to use namespaced models in order for the link_to helper to function properly.

e.g., in app/models/admin/comment.rb

class Admin::Comment < Comment   
end

Upvotes: 1

shingara
shingara

Reputation: 46914

You can use the name_route instead :

<%= link_to "Destroy", admin_topic_comment_path(@topic, comment), :method => :delete %>

Upvotes: 2

Related Questions