Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic Ruby on Rails Question about routing

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.

like image 466
mdrozdziel Avatar asked Mar 24 '10 08:03

mdrozdziel


2 Answers

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

like image 121
Ryan Bigg Avatar answered Sep 28 '22 18:09

Ryan Bigg


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.

like image 21
Teoulas Avatar answered Sep 28 '22 17:09

Teoulas