Reputation: 1946
I've many controllers in different namespaces. The controller_name method only returns the name of the controller, e.g. 'articles'. Is there any chance to get the full name like 'service/articles' (the articles controller is in the service namespace)?
I want do create a link to the index action of each controller via a globally used partial:
<%= link_to controller.display_name, { :controller => controller.controller_name, :action => "index"} %>
If this partial is rendered in a view in the 'users' namespace, I get an error: users/articles has no route (it should be service/articles).
Upvotes: 50
Views: 27004
Reputation: 11055
Have you tried:
controller_path
http://api.rubyonrails.org/classes/AbstractController/Base.html#method-i-controller_path
Upvotes: 109
Reputation: 40277
Instead of linking to the :controller, :action syntax, can you use nested resources
routes.rb
resources :users do
resources: articles
end
in your view
= link_to 'Articles', user_article_path(current_user)
The user_article will take care of your namespaces for you.
Upvotes: 3