Reputation: 77
I have a common partial for index where i want to insert some links. On this links i would like to use the named_routes, like
link_to "Say hi",things_path(:param1=>:hello, :params2=>:there)
but i dont know if things_path is users_path, places_path or reviews_path, because this partial is shared for all controllers. There is any way to get the named route associated to the class or the current controller. I want something like this
link_to "Say hi", path_of(@current_class)(:param1=>:hello, :params2=>:there)
Upvotes: 0
Views: 1057
Reputation: 32955
A path helper like cars_path is ultimately just a shortcut to setting :controller, :action and other params like :id. If you want to make the controller always equal to the current controller you can just say
link_to "Say hi", :action => "index", :param1=>:hello, :params2=>:there
Because the :controller option is omitted, it will be assumed to be the current controller.
Upvotes: 0
Reputation: 17408
There are several approaches to this. The simplest thing is to rely on polymorphic routing, such as: link_to "Say hi", @my_object
. In this case rails will look at the class of @my_object
and its current state (new_record?
) and use the appropriate route and restful action. Assuming your partial is named _foo.html.erb
then this can be as simple as
link_to 'Say hi', foo
... which is pretty awesome. This question has been asked before, here: Polymorphic Routes in Rails - in views but I guess it's hard to find answers without knowing the magic words "polymorphic routing" :-)
Upvotes: 1