Routes Question

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

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.

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

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.

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

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
[email protected]
wrote:

architecture to be more Rails-compliant.

Posted via http://www.ruby-forum.com/.


Ryan B.

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

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

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.

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 [email protected] 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.

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