From 4f7e2257fedb606a659bb639f26e9ef2baa85d14 Mon Sep 17 00:00:00 2001 From: Matt Jones Date: Thu, 12 Mar 2009 03:07:17 -0400 Subject: Fix several issues with code examples. - "chaining" layouts with :only/:except removed. See #2162 for discussion. - double render section now has working examples. Old version implied that calling render once and then falling through would give a DoubleRenderError. - complete example for layout proc. - cleaned up a few stray references to layouts ending in bare .erb (no format). --- .../guides/source/layouts_and_rendering.textile | 28 ++++++++++++++++------ 1 file changed, 21 insertions(+), 7 deletions(-) (limited to 'railties/guides/source') diff --git a/railties/guides/source/layouts_and_rendering.textile b/railties/guides/source/layouts_and_rendering.textile index 5e2cedcf0c..95c54a2295 100644 --- a/railties/guides/source/layouts_and_rendering.textile +++ b/railties/guides/source/layouts_and_rendering.textile @@ -316,7 +316,7 @@ Now, if the current user is a special user, they'll get a special layout when vi class ProductsController < ApplicationController - layout proc{ |controller| controller. + layout proc { |controller| controller.request.xhr? ? 'popup' : 'application' } # ... end @@ -327,13 +327,12 @@ Layouts specified at the controller level support +:only+ and +:except+ options class ProductsController < ApplicationController - layout "inventory", :only => :index layout "product", :except => [:index, :rss] #... end -With those declarations, the +inventory+ layout would be used only for the +index+ method, the +product+ layout would be used for everything else except the +rss+ method, and the +rss+ method will have its layout determined by the automatic layout rules. +With this declaration, the +product+ layout would be used for everything but the +rss+ and +index+ methods. h6. Layout Inheritance @@ -403,6 +402,7 @@ def show if @book.special? render :action => "special_show" end + render :action => "regular_show" end @@ -414,9 +414,23 @@ def show if @book.special? render :action => "special_show" and return end + render :action => "regular_show" end +Note that the implicit render done by ActionController detects if +render+ has been called, and thus avoids this error. So this code will work with problems: + + + 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. + h4. 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: @@ -455,7 +469,7 @@ end def show @book = Book.find(params[:id]) if @book.nil? - render :action => "index" and return + render :action => "index" end end @@ -470,7 +484,7 @@ end def show @book = Book.find(params[:id]) if @book.nil? - redirect_to :action => "index" and return + redirect_to :action => "index" end end @@ -915,7 +929,7 @@ You may find that your application requires a layout that differs slightly from Suppose you have the follow +ApplicationController+ layout: -* +app/views/layouts/application.erb+ +* +app/views/layouts/application.html.erb+ @@ -934,7 +948,7 @@ Suppose you have the follow +ApplicationController+ layout: On pages generated by +NewsController+, you want to hide the top menu and add a right menu: -* +app/views/layouts/news.erb+ +* +app/views/layouts/news.html.erb+ <% content_for :stylesheets do %> -- cgit v1.2.3