views:

160

answers:

2

In Ruby on Rails, routes.rb, if we create a "named route"

map.something ":a/:b", :controller => 'foobar'

it will also create something_path and something_url which are two methods usable in the controller and in the view. Does map.connect create something like that too? Otherwise, isn't map.connect somewhat disadvantaged in this way? I checked that connect_path and connect_url both aren't created automatically.

+1  A: 

You are correct in your thinking. map.connect does not create something_path and something_url. This is the purpose of named routes like map.something: To create "names," hence the name "named routes."

mathepic
in that case, url and path still exist... is there a quick to get those values?
動靜能量
You can try creating helper methods (in app/helpers/application_helper.rb) for generating the paths/urls you use often.
codinguser
No. That is the entire point of Rails Named Routes. If you need the path, you use a named route. If you don't care, you use connect.
mathepic
A: 

A named route can be thought of as a named map.connect route. map.connect just establishes a route that points to an action within a controller. But it would be a pain to call the route again and again everywhere. Using a named route is more readable. The advantage of map.connect is that it can be made to connect to any controllers action. If you read the routes.rb file carefully you see that the following two statements have the lowest priority:

Note: These default routes make all actions in every controller accessible via GET requests. You should
consider removing or commenting them out if you're using named routes and resources.
    map.connect ':controller/:action/:id'
    map.connect ':controller/:action/:id.:format'

If you comment out the above two lines you will not be able to reach any route except for the ones that you define using named routes/resources.

Shripad K