Generating URIs from Views and Controllers

Setting up these routes does more than connect URIs to your application. It also makes it easy for you to build connections between different parts of your application. Code can tell Rails what functionality it should point to, and Rails will generate an appropriate URI. There are many methods that generate URIs (form_for, link_to, and a host of others), but all of them rely on url_for, a helper method in the UrlModule class.

Pointing url_for in the Right Direction

The method signature for url_for doesn’t tell you very much about how to call it:

url_for(options = {})

Note

Remember, the parentheses around the method arguments are optional, as are the curly braces ({}) around the options hash.

Early Rails applications often called url_for by specifying all of the parts needed to create a URI—:controller, :action, and maybe :id:

url_for :action => 'bio', :controller => 'presidents', :id => '39'

This would produce a URI like:

/presidents/bio/39

There’s a simpler approach, though, if you just want to point to a particular object, say an @president object that has an id of 39:

url_for @president

Rails will check through its naming conventions, looking for a named route that matches the object specified. It will then call the named route’s _path helper method—in this case, probably president_path. The value returned by that helper will end up in the URI, likely as:

/presidents/39

To point to nested resources, you need to provide a little more information, two arguments in an array:

url_for [@student, @award]

And the result would be something like:

/students/1/awards/2

You can also point to a nested resource by calling its _path helper methods explicitly. For an award nested under a student, you could produce the same result with:

url_for student_award_path(@student, @award)

Adding Options

The options array is good for more than just specifying the pieces that will go into the URI. It lets you specify how the URI should appear, and add or override details. The available options include:

:anchor

Lets you add a fragment identifier to the end of your URI, separated by a # sign. This can be very effective when you want to point users to a specific item in a long list.

:escape

When true (which it is by default), all characters that are problematic for use in an HTML href attribute are escaped. If you just want to see the clean URI—probably for debugging—specify false.

:only_path

When true (which it is by default), url_for will only return the path part of the URI, the part that comes after the protocol, host name, and port. If you want a complete (absolute) URI, set this to false.

:trailing_slash

When true, this adds a slash at the end of URIs. While this may meet your expectations for working with directories (or things that look like directories) on the Web, it unfortunately breaks caching, so use it cautiously. This defaults to false.

:host and :protocol

These let you specify an particular host (including port number) and protocol. If these are specified, the full absolute URI will be returned, regardless of what :only_path was set to.

:user and :password

These two options must be used together. When present, Rails will incorporate them into the URI for inline HTTP authentication. This is generally not a good idea, as it’s not a very secure way to exchange credentials.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset