Prev | Current Page 319 | Next

Brad Ediger

"Advanced Rails"

id
to:
link_to 'Close', :controller => 'closures', :action => 'create',
:id => @case.id
The next major innovation in Rails routing was the prevalence of named routes. By
associating each URI with a name, you would get an easy way to refactor route URLs
without changing the inward links. This provided another layer of abstraction on top
of the actual URI parameters:
config/routes.rb
map.close_case '/cases/close/:id', :controller => 'cases', :action => 'close'
_case.rhtml
link_to 'Close', close_case_path(:id => @case.id)
Then, when the action moved, you could simply change the route without touching
the views:
map.close_case '/closures/create/:id', :controller => 'closures',
:action => 'create'
This greatly improved maintainability, but it increased verbosity. As there was
another layer of abstraction between URIs and actions, there was another file that
needed to be maintained, kept in sync with the rest of the code, and consulted when
tracing a request??™s path through the application.
The biggest problem, however, was that these routes tended to be redundant and
overly numerous. The problem was not so much that there was one named route for
each URI, but that the URIs referred to actions on resources instead of resources.
The HTTPphrase ???GET /cases/show/1??? is redundant. Both HTTP GET and the show
action mean ???give me this data.??? According to RESTful principles, that phrase
should be ???GET /cases/1,??? where the HTTPverb specifies the action and the URI
specifies the object of that action.


Pages:
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331