# Routes Question

**URL:** <https://www.ruby-forum.com/t/routes-question/125515>\
**Category:** Rails\
**Created:** [January 9, 2008, 9:43pm UTC](https://www.ruby-forum.com/t/routes-question/125515 "2008-01-09T21:43:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [January 9, 2008, 9:43pm UTC](https://www.ruby-forum.com/t/routes-question/125515/1 "2008-01-09T21:43:28Z")

</div>

Hi, I am using Rail 1.2. and tying to create a route like below.

map.connect ‘:controller/:action/:object/:id/’,  
:action =\> :action + '_’ + :object,  
:object =\> /[a-z|_]+/

For example, if user access main/show/person/1,  
I want to call the show\_person method in the main controller.

Are there any way I can refer current value of :action and :object in  
routes.rb?  
I have been googling example, but most of the show how to redirect to  
specific action and controller like blog/show/1.

Thanks in advance.

Glen

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [January 9, 2008, 9:57pm UTC](https://www.ruby-forum.com/t/routes-question/125515/2 "2008-01-09T21:57:19Z")

</div>

Glenn wrote:

> Hi, I am using Rail 1.2. and tying to create a route like below.
> 
> map.connect ‘:controller/:action/:object/:id/’,  
> :action =\> :action + '_’ + :object,  
> :object =\> /[a-z|_]+/
> 
> For example, if user access main/show/person/1,  
> I want to call the show\_person method in the main controller.
> 
> Are there any way I can refer current value of :action and :object in  
> routes.rb?  
> I have been googling example, but most of the show how to redirect to  
> specific action and controller like blog/show/1.
> 
> Thanks in advance.
> 
> Glen

I don’t know of a way to do that. I advise you to rethink your  
architecture to be more Rails-compliant.

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [January 9, 2008, 10:52pm UTC](https://www.ruby-forum.com/t/routes-question/125515/3 "2008-01-09T22:52:58Z")

</div>

Thank you for your response.

Hum… I have so many models, e.g., student controller handles models  
such as identity, address, email, courses, transcript, etc…

Do I need to create controller for each one? All records are shown in  
one single view called student/show/1. show.rhtml has AJAX-based  
tabs, so users can switch views easily.

Right now, I have actions like update\_identity, update\_address, etc.  
I was thinking if there are any way to make the URL clean.

I personally think that a controller could have multiple models if all  
models are related.

Glen

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [January 9, 2008, 10:57pm UTC](https://www.ruby-forum.com/t/routes-question/125515/4 "2008-01-09T22:57:06Z")

</div>

A controller can use and manage multiple models, but the “best practice”  
is to try to keep it to a controller per model. In cases where there’s a  
strict parent-child relationship where the child entities only have  
meaning within the context of their parent (like a student and her  
addresses) it can be OK to mix them in one controller.

However, if you’re updating student info separately from address info,  
you probably should have an AddressController to handle the address  
stuff. Even if it’s all one page with Ajax requests, it’s a more  
flexible architecture. For example, it would be pretty easy to expose a  
RESTful API with separate controllers.

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [January 10, 2008, 3:51am UTC](https://www.ruby-forum.com/t/routes-question/125515/5 "2008-01-10T03:51:26Z")

</div>

Thanks for your feedback. Now I started realizing the concept.

If I create a controller for each model, what is the good way to  
organize them in related group? Is possible to create controller in  
folder like folder/controller1/action? or do I need to use  
inheritance? There should be a common functions for each controller  
group.

Thank you,  
Glen

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [January 9, 2008, 10:08pm UTC](https://www.ruby-forum.com/t/routes-question/125515/6 "2008-01-09T22:08:49Z")

</div>

I agree with Jeremy.

The idea in Rails is that you have a controller per model in most casts.  
So  
if you had people & organisations, you would have two controllers  
people\_controller and organistions\_controller.

On Jan 10, 2008 7:27 AM, Jeremy Weiskotten  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > 
> 
> ## architecture to be more Rails-compliant.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [January 10, 2008, 11:39pm UTC](https://www.ruby-forum.com/t/routes-question/125515/7 "2008-01-10T23:39:27Z")

</div>

Thanks very much everybody.

After playing with my code, I manged to archive my initial goal.

I added following map in routes.rb.

map.connect ‘:controller/:dispatch\_action/:object/:id/’,  
:action =\> “dispatch”,  
:object =\> /[a-z|\_]+/

And, I added dispatch action to my application.rb  
def dispatch  
action = “#{params[:dispatch\_action]}\_#{params[:object]}”  
self.send(action)  
return false  
end

This way, when /student/update/transcript/1 is requested.  
The dispatch action is called and invokes update\_transcript method.

I am not sure this is a good way compared with creating controller for  
each model. Maybe I will check with rails 2.0 and see if there are  
any good features for grouping controllers.

Thanks.  
Glen

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [January 10, 2008, 4:02am UTC](https://www.ruby-forum.com/t/routes-question/125515/8 "2008-01-10T04:02:27Z")

</div>

Glenn wrote:

> Thanks for your feedback. Now I started realizing the concept.
> 
> If I create a controller for each model, what is the good way to  
> organize them in related group? Is possible to create controller in  
> folder like folder/controller1/action? or do I need to use  
> inheritance? There should be a common functions for each controller  
> group.
> 
> Thank you,  
> Glen

Most people just keep all of their controllers in the app/controllers  
directory. You can use inheritance (or modules) to share behavior  
between controllers.

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [January 10, 2008, 11:45pm UTC](https://www.ruby-forum.com/t/routes-question/125515/9 "2008-01-10T23:45:18Z")

</div>

The whole idea is to NOT group controllers. How you’ve done it is going  
against the rails conventions and one controller should not handle the  
CRUD  
actions for every object, but rather many different controllers should  
handle the CRUD actions for the many objects. So if you have a student  
model, you should have a students controller. If you have a transcripts  
model, you should have a transcripts controller. Believe me, there is  
stuff  
in Rails that will make it so much easier for you if you would just  
follow  
the conventions (and for anybody else that has to read your code)

On Jan 11, 2008 9:08 AM, Glenn [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am not sure this is a good way compared with creating controller for  
> each model. Maybe I will check with rails 2.0 and see if there are  
> any good features for grouping controllers.
> 
> Thanks.  
> Glen
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.
