I have a controller without any related model. This controller is to span some information from various models. I have lots of actions there, which define certain views on the page. What would be the best way to organize routes for this controller?
What I would like is to have /dashboard/something point to any action in the dashboard controller. Not actions like new/edit but arbitrary (showstats, etc).
With trial and error I made something like this:
map.dashboard 'dashboard/:action', :controller => 'dashboard', :action => :action
Now it is possible to access those URLs using the helper:
dashboard_url('actionname')
This approch seems to be working ok, but is this the way to go? I am not quite sure understand, how are the helper method names generated. How to generate same helper names as in basic controllers "action_controller_url" ? That would be more generic and made the code more consistent.
Thanks in advance.
You do not need to specify the :action => :action
key in the route, this is done for you already. Other than that, how you've done it is fine.
You can also specify it as a symbol: dashboard_url(:actionname)
, but you already knew that ;)
If you want helper methods for all your dashboard actions you should specify them all using named routes, for example:
map.example_dashboard 'dashboard/example', :controller => 'dashboard', :action => 'example'
map.another_dashboard 'dashboard/another', :controller => 'dashboard', :action => 'another'
Then rails will generate one _url and one _path helper for each named route. I prefer your approach, though, it's more flexible, and simple.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With