Layouts and Rendering in Rails ============================== This guide covers the basic layout features of Action Controller and Action View. By referring to this guide, you will be able to: * Use the various rendering methods built into Rails * Create layouts with multiple content sections * Use partials to DRY up your views * Use nested layouts (sub-templates) -------------------------------------------------------------------------------- Overview: How the Pieces Fit Together ------------------------------------- This guide focuses on the interaction between Controller and View in the Model-View-Controller triangle. As you know, the Controller is responsible for orchestrating the whole process of handling a request in Rails, though it normally hands off any heavy code to the Model. But then, when it's time to send a response back to the user, the Controller hands things off to the View. It's that handoff that is the subject of this guide. In broad strokes, this involves deciding what should be sent as the response and calling an appropriate method to create that response. If the response is a full-blown view, Rails also does some extra work to wrap the view in a layout and possibly to pull in partial views. You'll see all of those paths later in this guide. Creating Responses ------------------ From the controller's point of view, there are three ways to create an HTTP response: * Call `render` to create a full response to send back to the browser * Call `redirect_to` to send an HTTP redirect status code to the browser * Call `head` to create a response consisting solely of HTTP headers to send back to the browser ### Rendering by Default: Convention Over Configuration in Action You've heard that Rails promotes "convention over configuration". Default rendering is an excellent example of this. By default, controllers in Rails automatically render views with names that correspond to valid routes. For example, if you have this code in your `BooksController` class: ```ruby class BooksController < ApplicationController end ``` And the following in your routes file: ```ruby resources :books ``` And you have a view file `app/views/books/index.html.erb`: ```ruby
Title | Summary | |||
---|---|---|---|---|
<%= book.title %> | <%= book.content %> | <%= link_to "Show", book %> | <%= link_to "Edit", edit_book_path(book) %> | <%= link_to "Remove", book, :method => :delete, :data => { :confirm => "Are you sure?" } %> |
<%= p.name %>
<% end %>" ``` WARNING: There is seldom any good reason to use this option. Mixing ERB into your controllers defeats the MVC orientation of Rails and will make it harder for other developers to follow the logic of your project. Use a separate erb view instead. By default, inline rendering uses ERB. You can force it to use Builder instead with the `:type` option: ```ruby render :inline => "xml.p {'Horrid coding practice!'}", :type => :builder ``` #### Rendering Text You can send plain text - with no markup at all - back to the browser by using the `:text` option to `render`: ```ruby render :text => "OK" ``` TIP: Rendering pure text is most useful when you're responding to AJAX or web service requests that are expecting something other than proper HTML. NOTE: By default, if you use the `:text` option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the `:layout => true` option. #### Rendering JSON JSON is a JavaScript data format used by many AJAX libraries. Rails has built-in support for converting objects to JSON and rendering that JSON back to the browser: ```ruby render :json => @product ``` TIP: You don't need to call `to_json` on the object that you want to render. If you use the `:json` option, `render` will automatically call `to_json` for you. #### Rendering XML Rails also has built-in support for converting objects to XML and rendering that XML back to the caller: ```ruby render :xml => @product ``` TIP: You don't need to call `to_xml` on the object that you want to render. If you use the `:xml` option, `render` will automatically call `to_xml` for you. #### Rendering Vanilla JavaScript Rails can render vanilla JavaScript: ```ruby render :js => "alert('Hello Rails');" ``` This will send the supplied string to the browser with a MIME type of `text/javascript`. #### Options for `render` Calls to the `render` method generally accept four options: * `:content_type` * `:layout` * `:status` * `:location` ##### The `:content_type` Option By default, Rails will serve the results of a rendering operation with the MIME content-type of `text/html` (or `application/json` if you use the `:json` option, or `application/xml` for the `:xml` option.). There are times when you might like to change this, and you can do so by setting the `:content_type` option: ```ruby render :file => filename, :content_type => "application/rss" ``` ##### The `:layout` Option With most of the options to `render`, the rendered content is displayed as part of the current layout. You'll learn more about layouts and how to use them later in this guide. You can use the `:layout` option to tell Rails to use a specific file as the layout for the current action: ```ruby render :layout => "special_layout" ``` You can also tell Rails to render with no layout at all: ```ruby render :layout => false ``` ##### The `:status` Option Rails will automatically generate a response with the correct HTTP status code (in most cases, this is `200 OK`). You can use the `:status` option to change this: ```ruby render :status => 500 render :status => :forbidden ``` Rails understands both numeric and symbolic status codes. ##### The `:location` Option You can use the `:location` option to set the HTTP `Location` header: ```ruby render :xml => photo, :location => photo_url(photo) ``` #### Finding Layouts To find the current layout, Rails first looks for a file in `app/views/layouts` with the same base name as the controller. For example, rendering actions from the `PhotosController` class will use `app/views/layouts/photos.html.erb` (or `app/views/layouts/photos.builder`). If there is no such controller-specific layout, Rails will use `app/views/layouts/application.html.erb` or `app/views/layouts/application.builder`. If there is no `.erb` layout, Rails will use a `.builder` layout if one exists. Rails also provides several ways to more precisely assign specific layouts to individual controllers and actions. ##### Specifying Layouts for Controllers You can override the default layout conventions in your controllers by using the `layout` declaration. For example: ```ruby class ProductsController < ApplicationController layout "inventory" #... end ``` With this declaration, all of the views rendered by the products controller will use `app/views/layouts/inventory.html.erb` as their layout. To assign a specific layout for the entire application, use a `layout` declaration in your `ApplicationController` class: ```ruby class ApplicationController < ActionController::Base layout "main" #... end ``` With this declaration, all of the views in the entire application will use `app/views/layouts/main.html.erb` for their layout. ##### Choosing Layouts at Runtime You can use a symbol to defer the choice of layout until a request is processed: ```ruby class ProductsController < ApplicationController layout "products_layout" def show @product = Product.find(params[:id]) end private def products_layout @current_user.special? ? "special" : "products" end end ``` Now, if the current user is a special user, they'll get a special layout when viewing a product. You can even use an inline method, such as a Proc, to determine the layout. For example, if you pass a Proc object, the block you give the Proc will be given the `controller` instance, so the layout can be determined based on the current request: ```ruby class ProductsController < ApplicationController layout Proc.new { |controller| controller.request.xhr? ? "popup" : "application" } end ``` ##### Conditional Layouts Layouts specified at the controller level support the `:only` and `:except` options. These options take either a method name, or an array of method names, corresponding to method names within the controller: ```ruby class ProductsController < ApplicationController layout "product", :except => [:index, :rss] end ``` With this declaration, the `product` layout would be used for everything but the `rss` and `index` methods. ##### Layout Inheritance Layout declarations cascade downward in the hierarchy, and more specific layout declarations always override more general ones. For example: * `application_controller.rb` ```ruby class ApplicationController < ActionController::Base layout "main" end ``` * `posts_controller.rb` ```ruby class PostsController < ApplicationController end ``` * `special_posts_controller.rb` ```ruby class SpecialPostsController < PostsController layout "special" end ``` * `old_posts_controller.rb` ```ruby class OldPostsController < SpecialPostsController layout false def show @post = Post.find(params[:id]) end def index @old_posts = Post.older render :layout => "old" end # ... end ``` In this application: * In general, views will be rendered in the `main` layout * `PostsController#index` will use the `main` layout * `SpecialPostsController#index` will use the `special` layout * `OldPostsController#show` will use no layout at all * `OldPostsController#index` will use the `old` layout #### Avoiding Double Render Errors Sooner or later, most Rails developers will see the error message "Can only render or redirect once per action". While this is annoying, it's relatively easy to fix. Usually it happens because of a fundamental misunderstanding of the way that `render` works. For example, here's some code that will trigger this error: ```ruby def show @book = Book.find(params[:id]) if @book.special? render :action => "special_show" end render :action => "regular_show" end ``` If `@book.special?` evaluates to `true`, Rails will start the rendering process to dump the `@book` variable into the `special_show` view. But this will _not_ stop the rest of the code in the `show` action from running, and when Rails hits the end of the action, it will start to render the `regular_show` view - and throw an error. The solution is simple: make sure that you have only one call to `render` or `redirect` in a single code path. One thing that can help is `and return`. Here's a patched version of the method: ```ruby def show @book = Book.find(params[:id]) if @book.special? render :action => "special_show" and return end render :action => "regular_show" end ``` Make sure to use `and return` instead of `&& return` because `&& return` will not work due to the operator precedence in the Ruby Language. Note that the implicit render done by ActionController detects if `render` has been called, so the following will work without errors: ```ruby def show @book = Book.find(params[:id]) if @book.special? render :action => "special_show" end end ``` This will render a book with `special?` set with the `special_show` template, while other books will render with the default `show` template. ### Using `redirect_to` Another way to handle returning responses to an HTTP request is with `redirect_to`. As you've seen, `render` tells Rails which view (or other asset) to use in constructing a response. The `redirect_to` method does something completely different: it tells the browser to send a new request for a different URL. For example, you could redirect from wherever you are in your code to the index of photos in your application with this call: ```ruby redirect_to photos_url ``` You can use `redirect_to` with any arguments that you could use with `link_to` or `url_for`. There's also a special redirect that sends the user back to the page they just came from: ```ruby redirect_to :back ``` #### Getting a Different Redirect Status Code Rails uses HTTP status code 302, a temporary redirect, when you call `redirect_to`. If you'd like to use a different status code, perhaps 301, a permanent redirect, you can use the `:status` option: ```ruby redirect_to photos_path, :status => 301 ``` Just like the `:status` option for `render`, `:status` for `redirect_to` accepts both numeric and symbolic header designations. #### The Difference Between `render` and `redirect_to` Sometimes inexperienced developers think of `redirect_to` as a sort of `goto` command, moving execution from one place to another in your Rails code. This is _not_ correct. Your code stops running and waits for a new request for the browser. It just happens that you've told the browser what request it should make next, by sending back an HTTP 302 status code. Consider these actions to see the difference: ```ruby def index @books = Book.all end def show @book = Book.find_by_id(params[:id]) if @book.nil? render :action => "index" end end ``` With the code in this form, there will likely be a problem if the `@book` variable is `nil`. Remember, a `render :action` doesn't run any code in the target action, so nothing will set up the `@books` variable that the `index` view will probably require. One way to fix this is to redirect instead of rendering: ```ruby def index @books = Book.all end def show @book = Book.find_by_id(params[:id]) if @book.nil? redirect_to :action => :index end end ``` With this code, the browser will make a fresh request for the index page, the code in the `index` method will run, and all will be well. The only downside to this code is that it requires a round trip to the browser: the browser requested the show action with `/books/1` and the controller finds that there are no books, so the controller sends out a 302 redirect response to the browser telling it to go to `/books/`, the browser complies and sends a new request back to the controller asking now for the `index` action, the controller then gets all the books in the database and renders the index template, sending it back down to the browser which then shows it on your screen. While in a small application, this added latency might not be a problem, it is something to think about if response time is a concern. We can demonstrate one way to handle this with a contrived example: ```ruby def index @books = Book.all end def show @book = Book.find_by_id(params[:id]) if @book.nil? @books = Book.all render "index", :alert => "Your book was not found!" end end ``` This would detect that there are no books with the specified ID, populate the `@books` instance variable with all the books in the model, and then directly render the `index.html.erb` template, returning it to the browser with a flash alert message to tell the user what happened. ### Using `head` To Build Header-Only Responses The `head` method can be used to send responses with only headers to the browser. It provides a more obvious alternative to calling `render :nothing`. The `head` method takes one parameter, which is interpreted as a hash of header names and values. For example, you can return only an error header: ```ruby head :bad_request ``` This would produce the following header: ```bash HTTP/1.1 400 Bad Request Connection: close Date: Sun, 24 Jan 2010 12:15:53 GMT Transfer-Encoding: chunked Content-Type: text/html; charset=utf-8 X-Runtime: 0.013483 Set-Cookie: _blog_session=...snip...; path=/; HttpOnly Cache-Control: no-cache ``` Or you can use other HTTP headers to convey other information: ```ruby head :created, :location => photo_path(@photo) ``` Which would produce: ```bash HTTP/1.1 201 Created Connection: close Date: Sun, 24 Jan 2010 12:16:44 GMT Transfer-Encoding: chunked Location: /photos/1 Content-Type: text/html; charset=utf-8 X-Runtime: 0.083496 Set-Cookie: _blog_session=...snip...; path=/; HttpOnly Cache-Control: no-cache ``` Structuring Layouts ------------------- When Rails renders a view as a response, it does so by combining the view with the current layout, using the rules for finding the current layout that were covered earlier in this guide. Within a layout, you have access to three tools for combining different bits of output to form the overall response: * Asset tags * `yield` and `content_for` * Partials ### Asset Tag Helpers Asset tag helpers provide methods for generating HTML that link views to feeds, JavaScript, stylesheets, images, videos and audios. There are six asset tag helpers available in Rails: * `auto_discovery_link_tag` * `javascript_include_tag` * `stylesheet_link_tag` * `image_tag` * `video_tag` * `audio_tag` You can use these tags in layouts or other views, although the `auto_discovery_link_tag`, `javascript_include_tag`, and `stylesheet_link_tag`, are most commonly used in the `<head>` section of a layout. WARNING: The asset tag helpers do _not_ verify the existence of the assets at the specified locations; they simply assume that you know what you're doing and generate the link. #### Linking to Feeds with the `auto_discovery_link_tag` The `auto_discovery_link_tag` helper builds HTML that most browsers and newsreaders can use to detect the presence of RSS or Atom feeds. It takes the type of the link (`:rss` or `:atom`), a hash of options that are passed through to url_for, and a hash of options for the tag: ```erb <%= auto_discovery_link_tag(:rss, {:action => "feed"}, {:title => "RSS Feed"}) %> ``` There are three tag options available for the `auto_discovery_link_tag`: * `:rel` specifies the `rel` value in the link. The default value is "alternate". * `:type` specifies an explicit MIME type. Rails will generate an appropriate MIME type automatically. * `:title` specifies the title of the link. The default value is the uppercased `:type` value, for example, "ATOM" or "RSS". #### Linking to JavaScript Files with the `javascript_include_tag` The `javascript_include_tag` helper returns an HTML `script` tag for each source provided. If you are using Rails with the [Asset Pipeline](asset_pipeline.html) enabled, this helper will generate a link to `/assets/javascripts/` rather than `public/javascripts` which was used in earlier versions of Rails. This link is then served by the Sprockets gem, which was introduced in Rails 3.1. A JavaScript file within a Rails application or Rails engine goes in one of three locations: `app/assets`, `lib/assets` or `vendor/assets`. These locations are explained in detail in the [Asset Organization section in the Asset Pipeline Guide](asset_pipeline.html#asset-organization) You can specify a full path relative to the document root, or a URL, if you prefer. For example, to link to a JavaScript file that is inside a directory called `javascripts` inside of one of `app/assets`, `lib/assets` or `vendor/assets`, you would do this: ```erb <%= javascript_include_tag "main" %> ``` Rails will then output a `script` tag such as this: ```html ``` The request to this asset is then served by the Sprockets gem. To include multiple files such as `app/assets/javascripts/main.js` and `app/assets/javascripts/columns.js` at the same time: ```erb <%= javascript_include_tag "main", "columns" %> ``` To include `app/assets/javascripts/main.js` and `app/assets/javascripts/photos/columns.js`: ```erb <%= javascript_include_tag "main", "/photos/columns" %> ``` To include `http://example.com/main.js`: ```erb <%= javascript_include_tag "http://example.com/main.js" %> ``` If the application does not use the asset pipeline, the `:defaults` option loads jQuery by default: ```erb <%= javascript_include_tag :defaults %> ``` Outputting `script` tags such as this: ```html ``` These two files for jQuery, `jquery.js` and `jquery_ujs.js` must be placed inside `public/javascripts` if the application doesn't use the asset pipeline. These files can be downloaded from the [jquery-rails repository on GitHub](https://github.com/indirect/jquery-rails/tree/master/vendor/assets/javascripts) WARNING: If you are using the asset pipeline, this tag will render a `script` tag for an asset called `defaults.js`, which would not exist in your application unless you've explicitly created it. And you can in any case override the `:defaults` expansion in `config/application.rb`: ```ruby config.action_view.javascript_expansions[:defaults] = %w(foo.js bar.js) ``` You can also define new defaults: ```ruby config.action_view.javascript_expansions[:projects] = %w(projects.js tickets.js) ``` And use them by referencing them exactly like `:defaults`: ```erb <%= javascript_include_tag :projects %> ``` When using `:defaults`, if an `application.js` file exists in `public/javascripts` it will be included as well at the end. Also, if the asset pipeline is disabled, the `:all` expansion loads every JavaScript file in `public/javascripts`: ```erb <%= javascript_include_tag :all %> ``` Note that your defaults of choice will be included first, so they will be available to all subsequently included files. You can supply the `:recursive` option to load files in subfolders of `public/javascripts` as well: ```erb <%= javascript_include_tag :all, :recursive => true %> ``` If you're loading multiple JavaScript files, you can create a better user experience by combining multiple files into a single download. To make this happen in production, specify `:cache => true` in your `javascript_include_tag`: ```erb <%= javascript_include_tag "main", "columns", :cache => true %> ``` By default, the combined file will be delivered as `javascripts/all.js`. You can specify a location for the cached asset file instead: ```erb <%= javascript_include_tag "main", "columns", :cache => "cache/main/display" %> ``` You can even use dynamic paths such as `cache/#{current_site}/main/display`. #### Linking to CSS Files with the `stylesheet_link_tag` The `stylesheet_link_tag` helper returns an HTML `<link>` tag for each source provided. If you are using Rails with the "Asset Pipeline" enabled, this helper will generate a link to `/assets/stylesheets/`. This link is then processed by the Sprockets gem. A stylesheet file can be stored in one of three locations: `app/assets`, `lib/assets` or `vendor/assets`. You can specify a full path relative to the document root, or a URL. For example, to link to a stylesheet file that is inside a directory called `stylesheets` inside of one of `app/assets`, `lib/assets` or `vendor/assets`, you would do this: ```erb <%= stylesheet_link_tag "main" %> ``` To include `app/assets/stylesheets/main.css` and `app/assets/stylesheets/columns.css`: ```erb <%= stylesheet_link_tag "main", "columns" %> ``` To include `app/assets/stylesheets/main.css` and `app/assets/stylesheets/photos/columns.css`: ```erb <%= stylesheet_link_tag "main", "/photos/columns" %> ``` To include `http://example.com/main.css`: ```erb <%= stylesheet_link_tag "http://example.com/main.css" %> ``` By default, the `stylesheet_link_tag` creates links with `media="screen" rel="stylesheet"`. You can override any of these defaults by specifying an appropriate option (`:media`, `:rel`): ```erb <%= stylesheet_link_tag "main_print", :media => "print" %> ``` If the asset pipeline is disabled, the `all` option links every CSS file in `public/stylesheets`: ```erb <%= stylesheet_link_tag :all %> ``` You can supply the `:recursive` option to link files in subfolders of `public/stylesheets` as well: ```erb <%= stylesheet_link_tag :all, :recursive => true %> ``` If you're loading multiple CSS files, you can create a better user experience by combining multiple files into a single download. To make this happen in production, specify `:cache => true` in your `stylesheet_link_tag`: ```erb <%= stylesheet_link_tag "main", "columns", :cache => true %> ``` By default, the combined file will be delivered as `stylesheets/all.css`. You can specify a location for the cached asset file instead: ```erb <%= stylesheet_link_tag "main", "columns", :cache => "cache/main/display" %> ``` You can even use dynamic paths such as `cache/#{current_site}/main/display`. #### Linking to Images with the `image_tag` The `image_tag` helper builds an HTML `<img />` tag to the specified file. By default, files are loaded from `public/images`. WARNING: Note that you must specify the extension of the image. Previous versions of Rails would allow you to just use the image name and would append `.png` if no extension was given but Rails 3.0 does not. ```erb <%= image_tag "header.png" %> ``` You can supply a path to the image if you like: ```erb <%= image_tag "icons/delete.gif" %> ``` You can supply a hash of additional HTML options: ```erb <%= image_tag "icons/delete.gif", {:height => 45} %> ``` You can supply alternate text for the image which will be used if the user has images turned off in their browser. If you do not specify an alt text explicitly, it defaults to the file name of the file, capitalized and with no extension. For example, these two image tags would return the same code: ```erb <%= image_tag "home.gif" %> <%= image_tag "home.gif", :alt => "Home" %> ``` You can also specify a special size tag, in the format "{width}x{height}": ```erb <%= image_tag "home.gif", :size => "50x20" %> ``` In addition to the above special tags, you can supply a final hash of standard HTML options, such as `:class`, `:id` or `:name`: ```erb <%= image_tag "home.gif", :alt => "Go Home", :id => "HomeImage", :class => "nav_bar" %> ``` #### Linking to Videos with the `video_tag` The `video_tag` helper builds an HTML 5 `<video>` tag to the specified file. By default, files are loaded from `public/videos`. ```erb <%= video_tag "movie.ogg" %> ``` Produces ```erb ``` Like an `image_tag` you can supply a path, either absolute, or relative to the `public/videos` directory. Additionally you can specify the `:size => "#{width}x#{height}"` option just like an `image_tag`. Video tags can also have any of the HTML options specified at the end (`id`, `class` et al). The video tag also supports all of the `<video>` HTML options through the HTML options hash, including: * `:poster => "image_name.png"`, provides an image to put in place of the video before it starts playing. * `:autoplay => true`, starts playing the video on page load. * `:loop => true`, loops the video once it gets to the end. * `:controls => true`, provides browser supplied controls for the user to interact with the video. * `:autobuffer => true`, the video will pre load the file for the user on page load. You can also specify multiple videos to play by passing an array of videos to the `video_tag`: ```erb <%= video_tag ["trailer.ogg", "movie.ogg"] %> ``` This will produce: ```erb ``` #### Linking to Audio Files with the `audio_tag` The `audio_tag` helper builds an HTML 5 `<audio>` tag to the specified file. By default, files are loaded from `public/audios`. ```erb <%= audio_tag "music.mp3" %> ``` You can supply a path to the audio file if you like: ```erb <%= audio_tag "music/first_song.mp3" %> ``` You can also supply a hash of additional options, such as `:id`, `:class` etc. Like the `video_tag`, the `audio_tag` has special options: * `:autoplay => true`, starts playing the audio on page load * `:controls => true`, provides browser supplied controls for the user to interact with the audio. * `:autobuffer => true`, the audio will pre load the file for the user on page load. ### Understanding `yield` Within the context of a layout, `yield` identifies a section where content from the view should be inserted. The simplest way to use this is to have a single `yield`, into which the entire contents of the view currently being rendered is inserted: ```erb <%= yield %> ``` You can also create a layout with multiple yielding regions: ```erb <%= yield :head %> <%= yield %> ``` The main body of the view will always render into the unnamed `yield`. To render content into a named `yield`, you use the `content_for` method. ### Using the `content_for` Method The `content_for` method allows you to insert content into a named `yield` block in your layout. For example, this view would work with the layout that you just saw: ```erb <% content_for :head do %>Hello, Rails!
``` The result of rendering this page into the supplied layout would be this HTML: ```erbHello, Rails!
``` The `content_for` method is very helpful when your layout contains distinct regions such as sidebars and footers that should get their own blocks of content inserted. It's also useful for inserting tags that load page-specific JavaScript or css files into the header of an otherwise generic layout. ### Using Partials Partial templates - usually just called "partials" - are another device for breaking the rendering process into more manageable chunks. With a partial, you can move the code for rendering a particular piece of a response to its own file. #### Naming Partials To render a partial as part of a view, you use the `render` method within the view: ```ruby <%= render "menu" %> ``` This will render a file named `_menu.html.erb` at that point within the view being rendered. Note the leading underscore character: partials are named with a leading underscore to distinguish them from regular views, even though they are referred to without the underscore. This holds true even when you're pulling in a partial from another folder: ```ruby <%= render "shared/menu" %> ``` That code will pull in the partial from `app/views/shared/_menu.html.erb`. #### Using Partials to Simplify Views One way to use partials is to treat them as the equivalent of subroutines: as a way to move details out of a view so that you can grasp what's going on more easily. For example, you might have a view that looked like this: ```erb <%= render "shared/ad_banner" %>Here are a few of our fine products:
... <%= render "shared/footer" %> ``` Here, the `_ad_banner.html.erb` and `_footer.html.erb` partials could contain content that is shared among many pages in your application. You don't need to see the details of these sections when you're concentrating on a particular page. TIP: For content that is shared among all pages in your application, you can use partials directly from layouts. #### Partial Layouts A partial can use its own layout file, just as a view can use a layout. For example, you might call a partial like this: ```erb <%= render :partial => "link_area", :layout => "graybar" %> ``` This would look for a partial named `_link_area.html.erb` and render it using the layout `_graybar.html.erb`. Note that layouts for partials follow the same leading-underscore naming as regular partials, and are placed in the same folder with the partial that they belong to (not in the master `layouts` folder). Also note that explicitly specifying `:partial` is required when passing additional options such as `:layout`. #### Passing Local Variables You can also pass local variables into partials, making them even more powerful and flexible. For example, you can use this technique to reduce duplication between new and edit pages, while still keeping a bit of distinct content: * `new.html.erb` ```erb
Zone name
<%= f.text_field :name %>
<%= f.submit %>
<% end %> ``` Although the same partial will be rendered into both views, Action View's submit helper will return "Create Zone" for the new action and "Update Zone" for the edit action. Every partial also has a local variable with the same name as the partial (minus the underscore). You can pass an object in to this local variable via the `:object` option: ```erb <%= render :partial => "customer", :object => @new_customer %> ``` Within the `customer` partial, the `customer` variable will refer to `@new_customer` from the parent view. WARNING: In previous versions of Rails, the default local variable would look for an instance variable with the same name as the partial in the parent. This behavior was deprecated in 2.3 and has been removed in Rails 3.0. If you have an instance of a model to render into a partial, you can use a shorthand syntax: ```erb <%= render @customer %> ``` Assuming that the `@customer` instance variable contains an instance of the `Customer` model, this will use `_customer.html.erb` to render it and will pass the local variable `customer` into the partial which will refer to the `@customer` instance variable in the parent view. #### Rendering Collections Partials are very useful in rendering collections. When you pass a collection to a partial via the `:collection` option, the partial will be inserted once for each member in the collection: * `index.html.erb` ```erbProduct Name: <%= product.name %>
``` When a partial is called with a pluralized collection, then the individual instances of the partial have access to the member of the collection being rendered via a variable named after the partial. In this case, the partial is `_product`, and within the `_product` partial, you can refer to `product` to get the instance that is being rendered. In Rails 3.0, there is also a shorthand for this. Assuming `@products` is a collection of `product` instances, you can simply write this in the `index.html.erb` to produce the same result: ```erbCustomer: <%= customer.name %>
``` * `employees/_employee.html.erb` ```erbEmployee: <%= employee.name %>
``` In this case, Rails will use the customer or employee partials as appropriate for each member of the collection. In the event that the collection is empty, `render` will return nil, so it should be fairly simple to provide alternative content. ```erb