views:

99

answers:

3

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.

+5  A: 

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 ;)

Ryan Bigg
Is there a way to reformat the helper, or provide a custom one? IE: actionname_dashboard_url instead dashboard_url(:actionname)?
mdrozdziel
You would need to define a new route for every single one. The singular route method is a couple of characters longer, but results in less code overall.
Ryan Bigg
Yeah, I though theres some automatic mechanizm, which generates the helpers on the fly, but since thats not the case, I will just stick to the dashboard_url(:actionname) convention.
mdrozdziel
+2  A: 

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.

Teoulas
Wouldn't the index page for the OrdersController and ClientsController be a better place to serve the "dashboard" of these?
Ryan Bigg
Definitely, I was just making up action names for my example. I'll change it to something else.
Teoulas
A: 

How to generate same helper names as in basic controllers "action_controller_url" ?

  map.action_controller(
    'action_controller',
    :controller => 'controller',
    :action => 'action',
    :conditions => { :method => :get }  # You can restrict the HTTP reqs allowed
  )
Ethan
This isn't really the answer. If I will have 50 actions, then I will have to copy this code 50 times or create some werid loops over map...
mdrozdziel