--- !ruby/object:RI::ClassDescription 
attributes: []

class_methods: []

comment: 
- !ruby/struct:SM::Flow::P 
  body: "In <b>routes.rb</b> one defines URL-to-controller mappings, but the reverse is also possible: an URL can be generated from one of your routing definitions. URL generation functionality is centralized in this module."
- !ruby/struct:SM::Flow::P 
  body: See ActionController::Routing and ActionController::Resources for general information about routing and routes.rb.
- !ruby/struct:SM::Flow::P 
  body: <b>Tip:</b> If you need to generate URLs from your models or some other place, then ActionController::UrlWriter is what you're looking for. Read on for an introduction.
- !ruby/struct:SM::Flow::H 
  level: 2
  text: URL generation from parameters
- !ruby/struct:SM::Flow::P 
  body: "As you may know, some functions - such as ActionController::Base#url_for and ActionView::Helpers::UrlHelper#link_to, can generate URLs given a set of parameters. For example, you've probably had the chance to write code like this in one of your views:"
- !ruby/struct:SM::Flow::VERB 
  body: "  &lt;%= link_to('Click here', :controller =&gt; 'users',\n          :action =&gt; 'new', :message =&gt; 'Welcome!') %&gt;\n\n  #=&gt; Generates a link to: /users/new?message=Welcome%21\n"
- !ruby/struct:SM::Flow::P 
  body: "link_to, and all other functions that require URL generation functionality, actually use ActionController::UrlWriter under the hood. And in particular, they use the ActionController::UrlWriter#url_for method. One can generate the same path as the above example by using the following code:"
- !ruby/struct:SM::Flow::VERB 
  body: "  include UrlWriter\n  url_for(:controller =&gt; 'users',\n          :action =&gt; 'new',\n          :message =&gt; 'Welcome!',\n          :only_path =&gt; true)\n  # =&gt; &quot;/users/new?message=Welcome%21&quot;\n"
- !ruby/struct:SM::Flow::P 
  body: "Notice the <tt>:only_path =&gt; true</tt> part. This is because UrlWriter has no information about the website hostname that your Rails app is serving. So if you want to include the hostname as well, then you must also pass the <tt>:host</tt> argument:"
- !ruby/struct:SM::Flow::VERB 
  body: "  include UrlWriter\n  url_for(:controller =&gt; 'users',\n          :action =&gt; 'new',\n          :message =&gt; 'Welcome!',\n          :host =&gt; 'www.example.com')        # Changed this.\n  # =&gt; &quot;http://www.example.com/users/new?message=Welcome%21&quot;\n"
- !ruby/struct:SM::Flow::P 
  body: By default, all controllers and views have access to a special version of url_for, that already knows what the current hostname is. So if you use url_for in your controllers or your views, then you don't need to explicitly pass the <tt>:host</tt> argument.
- !ruby/struct:SM::Flow::P 
  body: For convenience reasons, mailers provide a shortcut for ActionController::UrlWriter#url_for. So within mailers, you only have to type 'url_for' instead of 'ActionController::UrlWriter#url_for' in full. However, mailers don't have hostname information, and what's why you'll still have to specify the <tt>:host</tt> argument when generating URLs in mailers.
- !ruby/struct:SM::Flow::H 
  level: 2
  text: URL generation for named routes
- !ruby/struct:SM::Flow::P 
  body: "UrlWriter also allows one to access methods that have been auto-generated from named routes. For example, suppose that you have a 'users' resource in your <b>routes.rb</b>:"
- !ruby/struct:SM::Flow::VERB 
  body: "  map.resources :users\n"
- !ruby/struct:SM::Flow::P 
  body: This generates, among other things, the method <tt>users_path</tt>. By default, this method is accessible from your controllers, views and mailers. If you need to access this auto-generated method from other places (such as a model), then you can do that in two ways.
- !ruby/struct:SM::Flow::P 
  body: "The first way is to include ActionController::UrlWriter in your class:"
- !ruby/struct:SM::Flow::VERB 
  body: "  class User &lt; ActiveRecord::Base\n    include ActionController::UrlWriter         # !!!\n\n    def name=(value)\n      write_attribute('name', value)\n      write_attribute('base_uri', users_path)   # !!!\n    end\n  end\n"
- !ruby/struct:SM::Flow::P 
  body: "The second way is to access them through ActionController::UrlWriter. The autogenerated named routes methods are available as class methods:"
- !ruby/struct:SM::Flow::VERB 
  body: "  class User &lt; ActiveRecord::Base\n    def name=(value)\n      write_attribute('name', value)\n      path = ActionController::UrlWriter.users_path   # !!!\n      write_attribute('base_uri', path)               # !!!\n    end\n  end\n"
constants: []

full_name: ActionController::UrlWriter
includes: []

instance_methods: 
- !ruby/object:RI::MethodSummary 
  name: url_for
name: UrlWriter
superclass: 
