In Ruby on Rails, routes.rb, if map.something will create something_path and something_url, does map.connect create something like that too?
开发者_JAVA百科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.
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."
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.
精彩评论