diff options
author | Matt Jones <al2o3cr@gmail.com> | 2009-03-12 03:07:17 -0400 |
---|---|---|
committer | Matt Jones <al2o3cr@gmail.com> | 2009-03-12 03:07:17 -0400 |
commit | 4f7e2257fedb606a659bb639f26e9ef2baa85d14 (patch) | |
tree | 49c579d9dd960a905f49a36c3e00a01d58dec943 /railties/guides | |
parent | 724eb9e6121b36e61f373ba4bdcce29f3d0c78da (diff) | |
download | rails-4f7e2257fedb606a659bb639f26e9ef2baa85d14.tar.gz rails-4f7e2257fedb606a659bb639f26e9ef2baa85d14.tar.bz2 rails-4f7e2257fedb606a659bb639f26e9ef2baa85d14.zip |
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).
Diffstat (limited to 'railties/guides')
-rw-r--r-- | railties/guides/source/layouts_and_rendering.textile | 28 |
1 files changed, 21 insertions, 7 deletions
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 <ruby> class ProductsController < ApplicationController - layout proc{ |controller| controller. + layout proc { |controller| controller.request.xhr? ? 'popup' : 'application' } # ... end </ruby> @@ -327,13 +327,12 @@ Layouts specified at the controller level support +:only+ and +:except+ options <ruby> class ProductsController < ApplicationController - layout "inventory", :only => :index layout "product", :except => [:index, :rss] #... end </ruby> -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 </ruby> @@ -414,9 +414,23 @@ def show if @book.special? render :action => "special_show" and return end + render :action => "regular_show" end </ruby> +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: + +<ruby> + def show + @book = Book.find(params[:id]) + if @book.special? + render :action => "special_show" + end + end +</ruby> + +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 </ruby> @@ -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 </ruby> @@ -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+ <erb> <html> @@ -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+ <erb> <% content_for :stylesheets do %> |