Prev | Current Page 322 | Next

Brad Ediger

"Advanced Rails"

In addition, there is a hash_for_ variant
that returns a hash of parameters ready to be passed into url_for, rather than returning
the URI directly as a string:
>> app.hash_for_people_path
=> {:action=>"index", :controller=>"people", :only_path=>true,
:use_route=>"people"}
>> app.hash_for_people_url
=> {:action=>"index", :controller=>"people", :only_path=>false,
:use_route=>"people"}
The app variable is an instance of ApplicationController::
Integration::Session (used for Rails integration testing) that is made
available at the Rails console. It can be used to troubleshoot controller
issues (such as routing), and even to make mock HTTPrequests of
the application, just as in integration testing:
$ script/console
Loading development environment.
>> app.get '/hello/world'
=> 200
>> app.response.headers
=> {"Status"=>"200 OK", "type"=>"text/html; charset=utf-8",
"cookie"=>[], "Cache-Control"=>"no-cache",
"Content-Length"=>13}
>> app.response.body
=> "Hello, world!"
Extra query string parameters can be added to the named routes, but in the case of
member routes, the member object or ID must be included in the hash as an :id
parameter:
$ script/console
Loading development environment.
>> app.person_path(1)
=> "/people/1"
>> app.person_path(:id => 1)
new_person_path /people/new {}
edit_person_path(1)
edit_person_path(@person)
/people/1/edit {:id => 1}
Named route URI Params
RESTful Rails | 213
=> "/people/1"
>> app.


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