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

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.

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