rails 2.3.8 → 2.3.9.pre
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rails might be problematic. Click here for more details.
- data/CHANGELOG +3 -2
- data/Rakefile +6 -7
- data/builtin/rails_info/rails/info.rb +1 -1
- data/environments/boot.rb +6 -2
- data/environments/environment.rb +1 -1
- data/lib/initializer.rb +44 -20
- data/lib/rails/plugin.rb +1 -1
- data/lib/rails/plugin/loader.rb +3 -3
- data/lib/rails/version.rb +1 -1
- data/lib/rails_generator/generators/components/migration/migration_generator.rb +1 -1
- data/lib/rails_generator/generators/components/plugin/templates/test_helper.rb +2 -1
- metadata +25 -124
- data/guides/files/javascripts/code_highlighter.js +0 -188
- data/guides/files/javascripts/guides.js +0 -8
- data/guides/files/javascripts/highlighters.js +0 -90
- data/guides/files/stylesheets/main.css +0 -441
- data/guides/files/stylesheets/print.css +0 -52
- data/guides/files/stylesheets/reset.css +0 -43
- data/guides/files/stylesheets/style.css +0 -13
- data/guides/files/stylesheets/syntax.css +0 -31
- data/guides/images/belongs_to.png +0 -0
- data/guides/images/book_icon.gif +0 -0
- data/guides/images/bullet.gif +0 -0
- data/guides/images/chapters_icon.gif +0 -0
- data/guides/images/check_bullet.gif +0 -0
- data/guides/images/credits_pic_blank.gif +0 -0
- data/guides/images/csrf.png +0 -0
- data/guides/images/customized_error_messages.png +0 -0
- data/guides/images/error_messages.png +0 -0
- data/guides/images/feature_tile.gif +0 -0
- data/guides/images/footer_tile.gif +0 -0
- data/guides/images/fxn.jpg +0 -0
- data/guides/images/grey_bullet.gif +0 -0
- data/guides/images/habtm.png +0 -0
- data/guides/images/has_many.png +0 -0
- data/guides/images/has_many_through.png +0 -0
- data/guides/images/has_one.png +0 -0
- data/guides/images/has_one_through.png +0 -0
- data/guides/images/header_backdrop.png +0 -0
- data/guides/images/header_tile.gif +0 -0
- data/guides/images/i18n/demo_localized_pirate.png +0 -0
- data/guides/images/i18n/demo_translated_en.png +0 -0
- data/guides/images/i18n/demo_translated_pirate.png +0 -0
- data/guides/images/i18n/demo_translation_missing.png +0 -0
- data/guides/images/i18n/demo_untranslated.png +0 -0
- data/guides/images/icons/README +0 -5
- data/guides/images/icons/callouts/1.png +0 -0
- data/guides/images/icons/callouts/10.png +0 -0
- data/guides/images/icons/callouts/11.png +0 -0
- data/guides/images/icons/callouts/12.png +0 -0
- data/guides/images/icons/callouts/13.png +0 -0
- data/guides/images/icons/callouts/14.png +0 -0
- data/guides/images/icons/callouts/15.png +0 -0
- data/guides/images/icons/callouts/2.png +0 -0
- data/guides/images/icons/callouts/3.png +0 -0
- data/guides/images/icons/callouts/4.png +0 -0
- data/guides/images/icons/callouts/5.png +0 -0
- data/guides/images/icons/callouts/6.png +0 -0
- data/guides/images/icons/callouts/7.png +0 -0
- data/guides/images/icons/callouts/8.png +0 -0
- data/guides/images/icons/callouts/9.png +0 -0
- data/guides/images/icons/caution.png +0 -0
- data/guides/images/icons/example.png +0 -0
- data/guides/images/icons/home.png +0 -0
- data/guides/images/icons/important.png +0 -0
- data/guides/images/icons/next.png +0 -0
- data/guides/images/icons/note.png +0 -0
- data/guides/images/icons/prev.png +0 -0
- data/guides/images/icons/tip.png +0 -0
- data/guides/images/icons/up.png +0 -0
- data/guides/images/icons/warning.png +0 -0
- data/guides/images/nav_arrow.gif +0 -0
- data/guides/images/polymorphic.png +0 -0
- data/guides/images/posts_index.png +0 -0
- data/guides/images/rails_guides_logo.gif +0 -0
- data/guides/images/rails_logo_remix.gif +0 -0
- data/guides/images/rails_welcome.png +0 -0
- data/guides/images/session_fixation.png +0 -0
- data/guides/images/tab_grey.gif +0 -0
- data/guides/images/tab_info.gif +0 -0
- data/guides/images/tab_note.gif +0 -0
- data/guides/images/tab_red.gif +0 -0
- data/guides/images/tab_yellow.gif +0 -0
- data/guides/images/tab_yellow.png +0 -0
- data/guides/images/validation_error_messages.png +0 -0
- data/guides/rails_guides.rb +0 -42
- data/guides/rails_guides/generator.rb +0 -138
- data/guides/rails_guides/helpers.rb +0 -34
- data/guides/rails_guides/indexer.rb +0 -55
- data/guides/rails_guides/textile_extensions.rb +0 -41
- data/guides/source/2_2_release_notes.textile +0 -422
- data/guides/source/2_3_release_notes.textile +0 -610
- data/guides/source/action_controller_overview.textile +0 -776
- data/guides/source/action_mailer_basics.textile +0 -424
- data/guides/source/active_record_basics.textile +0 -135
- data/guides/source/active_record_querying.textile +0 -969
- data/guides/source/activerecord_validations_callbacks.textile +0 -1086
- data/guides/source/association_basics.textile +0 -1781
- data/guides/source/caching_with_rails.textile +0 -524
- data/guides/source/command_line.textile +0 -589
- data/guides/source/configuring.textile +0 -234
- data/guides/source/contribute.textile +0 -71
- data/guides/source/contributing_to_rails.textile +0 -239
- data/guides/source/credits.erb.textile +0 -52
- data/guides/source/debugging_rails_applications.textile +0 -709
- data/guides/source/form_helpers.textile +0 -766
- data/guides/source/getting_started.textile +0 -1297
- data/guides/source/i18n.textile +0 -912
- data/guides/source/index.erb.textile +0 -124
- data/guides/source/layout.html.erb +0 -103
- data/guides/source/layouts_and_rendering.textile +0 -979
- data/guides/source/migrations.textile +0 -591
- data/guides/source/nested_model_forms.textile +0 -222
- data/guides/source/performance_testing.textile +0 -531
- data/guides/source/plugins.textile +0 -1512
- data/guides/source/rails_on_rack.textile +0 -309
- data/guides/source/routing.textile +0 -903
- data/guides/source/security.textile +0 -986
- data/guides/source/testing.textile +0 -951
@@ -1,124 +0,0 @@
|
|
1
|
-
<% content_for :header_section do %>
|
2
|
-
h2. Ruby on Rails guides
|
3
|
-
|
4
|
-
These guides are designed to make you immediately productive with Rails, and to help you understand how all of the pieces fit together. There are two different versions of the Guides site, and you should be sure to use the one that applies to your situation:
|
5
|
-
|
6
|
-
* "Current Release version":http://guides.rubyonrails.org - based on Rails 2.3
|
7
|
-
* "Edge version":http://guides.rails.info - based on the current Rails "master branch":http://github.com/rails/rails/tree/master
|
8
|
-
|
9
|
-
<% end %>
|
10
|
-
|
11
|
-
<% content_for :index_section do %>
|
12
|
-
<div id="subCol">
|
13
|
-
<dl>
|
14
|
-
<dd class="warning">Rails Guides are a result of the ongoing "Guides hackfest":http://hackfest.rubyonrails.org and a work in progress.</dd>
|
15
|
-
<dd class="ticket">Guides marked with this icon are currently being worked on. While they might still be useful to you, they may contain incomplete information and even errors. You can help by reviewing them and posting your comments and corrections at the respective Lighthouse ticket.</dd>
|
16
|
-
</dl>
|
17
|
-
</div>
|
18
|
-
<% end %>
|
19
|
-
|
20
|
-
h3. Start Here
|
21
|
-
|
22
|
-
<dl>
|
23
|
-
<% guide('Getting Started with Rails', 'getting_started.html') do %>
|
24
|
-
Everything you need to know to install Rails and create your first application.
|
25
|
-
<% end %>
|
26
|
-
</dl>
|
27
|
-
|
28
|
-
h3. Models
|
29
|
-
|
30
|
-
<dl>
|
31
|
-
<% guide("Rails Database Migrations", 'migrations.html') do %>
|
32
|
-
This guide covers how you can use Active Record migrations to alter your database in a structured and organized manner.
|
33
|
-
<% end %>
|
34
|
-
|
35
|
-
<% guide("Active Record Validations and Callbacks", 'activerecord_validations_callbacks.html') do %>
|
36
|
-
This guide covers how you can use Active Record validations and callbacks.
|
37
|
-
<% end %>
|
38
|
-
|
39
|
-
<% guide("Active Record Associations", 'association_basics.html') do %>
|
40
|
-
This guide covers all the associations provided by Active Record.
|
41
|
-
<% end %>
|
42
|
-
|
43
|
-
<% guide("Active Record Query Interface", 'active_record_querying.html') do %>
|
44
|
-
This guide covers the database query interface provided by Active Record.
|
45
|
-
<% end %>
|
46
|
-
</dl>
|
47
|
-
|
48
|
-
h3. Views
|
49
|
-
|
50
|
-
<dl>
|
51
|
-
<% guide("Layouts and Rendering in Rails", 'layouts_and_rendering.html') do %>
|
52
|
-
This guide covers the basic layout features of Action Controller and Action View, including rendering and redirecting, using content_for blocks, and working with partials.
|
53
|
-
<% end %>
|
54
|
-
|
55
|
-
<% guide("Action View Form Helpers", 'form_helpers.html', :ticket => 1) do %>
|
56
|
-
Guide to using built in Form helpers.
|
57
|
-
<% end %>
|
58
|
-
</dl>
|
59
|
-
|
60
|
-
h3. Controllers
|
61
|
-
|
62
|
-
<dl>
|
63
|
-
<% guide("Action Controller Overview", 'action_controller_overview.html') do %>
|
64
|
-
This guide covers how controllers work and how they fit into the request cycle in your application. It includes sessions, filters, and cookies, data streaming, and dealing with exceptions raised by a request, among other topics.
|
65
|
-
<% end %>
|
66
|
-
|
67
|
-
<% guide("Rails Routing from the Outside In", 'routing.html') do %>
|
68
|
-
This guide covers the user-facing features of Rails routing. If you want to understand how to use routing in your own Rails applications, start here.
|
69
|
-
<% end %>
|
70
|
-
</dl>
|
71
|
-
|
72
|
-
h3. Digging Deeper
|
73
|
-
|
74
|
-
<dl>
|
75
|
-
|
76
|
-
<% guide("Rails on Rack", 'rails_on_rack.html') do %>
|
77
|
-
This guide covers Rails integration with Rack and interfacing with other Rack components.
|
78
|
-
<% end %>
|
79
|
-
|
80
|
-
<% guide("Rails Internationalization API", 'i18n.html') do %>
|
81
|
-
This guide covers how to add internationalization to your applications. Your application will be able to translate content to different languages, change pluralization rules, use correct date formats for each country and so on.
|
82
|
-
<% end %>
|
83
|
-
|
84
|
-
<% guide("Action Mailer Basics", 'action_mailer_basics.html', :ticket => 25) do %>
|
85
|
-
This guide describes how to use Action Mailer to send and receive emails.
|
86
|
-
<% end %>
|
87
|
-
|
88
|
-
<% guide("Testing Rails Applications", 'testing.html', :ticket => 8) do %>
|
89
|
-
This is a rather comprehensive guide to doing both unit and functional tests in Rails. It covers everything from “What is a test?” to the testing APIs. Enjoy.
|
90
|
-
<% end %>
|
91
|
-
|
92
|
-
<% guide("Securing Rails Applications", 'security.html') do %>
|
93
|
-
This guide describes common security problems in web applications and how to avoid them with Rails.
|
94
|
-
<% end %>
|
95
|
-
|
96
|
-
<% guide("Debugging Rails Applications", 'debugging_rails_applications.html') do %>
|
97
|
-
This guide describes how to debug Rails applications. It covers the different ways of achieving this and how to understand what is happening "behind the scenes" of your code.
|
98
|
-
<% end %>
|
99
|
-
|
100
|
-
<% guide("Performance Testing Rails Applications", 'performance_testing.html') do %>
|
101
|
-
This guide covers the various ways of performance testing a Ruby on Rails application.
|
102
|
-
<% end %>
|
103
|
-
|
104
|
-
<% guide("The Basics of Creating Rails Plugins", 'plugins.html', :ticket => 32) do %>
|
105
|
-
This guide covers how to build a plugin to extend the functionality of Rails.
|
106
|
-
<% end %>
|
107
|
-
|
108
|
-
<% guide("Configuring Rails Applications", 'configuring.html') do %>
|
109
|
-
This guide covers the basic configuration settings for a Rails application.
|
110
|
-
<% end %>
|
111
|
-
|
112
|
-
<% guide("Rails Command Line Tools and Rake tasks", 'command_line.html', :ticket => 29) do %>
|
113
|
-
This guide covers the command line tools and rake tasks provided by Rails.
|
114
|
-
<% end %>
|
115
|
-
|
116
|
-
<% guide("Caching with Rails", 'caching_with_rails.html', :ticket => 10) do %>
|
117
|
-
Various caching techniques provided by Rails.
|
118
|
-
<% end %>
|
119
|
-
|
120
|
-
<% guide("Contributing to Rails", 'contributing_to_rails.html') do %>
|
121
|
-
Rails is not "somebody else's framework." This guide covers a variety of ways that you can get involved in the ongoing development of Rails.
|
122
|
-
<% end %>
|
123
|
-
|
124
|
-
</dl>
|
@@ -1,103 +0,0 @@
|
|
1
|
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
2
|
-
<head>
|
3
|
-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
4
|
-
|
5
|
-
<title><%= yield(:page_title) || 'Ruby on Rails guides' %></title>
|
6
|
-
|
7
|
-
<link rel="stylesheet" type="text/css" href="files/stylesheets/style.css" />
|
8
|
-
<link rel="stylesheet" type="text/css" href="files/stylesheets/syntax.css" />
|
9
|
-
<link rel="stylesheet" type="text/css" href="files/stylesheets/print.css" media="print" />
|
10
|
-
|
11
|
-
<script type="text/javascript" src="files/javascripts/guides.js"></script>
|
12
|
-
<script type="text/javascript" src="files/javascripts/code_highlighter.js"></script>
|
13
|
-
<script type="text/javascript" src="files/javascripts/highlighters.js"></script>
|
14
|
-
|
15
|
-
</head>
|
16
|
-
<body class="guide">
|
17
|
-
<div id="topNav">
|
18
|
-
<div class="wrapper">
|
19
|
-
<strong>More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
|
20
|
-
<a href="http://rubyonrails.org/">Overview</a> |
|
21
|
-
<a href="http://rubyonrails.org/download">Download</a> |
|
22
|
-
<a href="http://rubyonrails.org/deploy">Deploy</a> |
|
23
|
-
<a href="http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/overview">Code</a> |
|
24
|
-
<a href="http://rubyonrails.org/screencasts">Screencasts</a> |
|
25
|
-
<a href="http://rubyonrails.org/documentation">Documentation</a> |
|
26
|
-
<a href="http://rubyonrails.org/ecosystem">Ecosystem</a> |
|
27
|
-
<a href="http://rubyonrails.org/community">Community</a> |
|
28
|
-
<a href="http://weblog.rubyonrails.org/">Blog</a>
|
29
|
-
</div>
|
30
|
-
</div>
|
31
|
-
<div id="header">
|
32
|
-
<div class="wrapper clearfix">
|
33
|
-
<h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
|
34
|
-
<p class="hide"><a href="#mainCol">Skip navigation</a>.</p>
|
35
|
-
<ul class="nav">
|
36
|
-
<li><a href="index.html">Home</a></li>
|
37
|
-
<li class="index"><a href="index.html" onclick="guideMenu(); return false;" id="guidesMenu">Guides Index</a>
|
38
|
-
<div id="guides" class="clearfix" style="display: none;">
|
39
|
-
<hr />
|
40
|
-
<dl class="L">
|
41
|
-
<dt>Start Here</dt>
|
42
|
-
<dd><a href="getting_started.html">Getting Started with Rails</a></dd>
|
43
|
-
<dt>Models</dt>
|
44
|
-
<dd><a href="migrations.html">Rails Database Migrations</a></dd>
|
45
|
-
<dd><a href="activerecord_validations_callbacks.html">Active Record Validations and Callbacks</a></dd>
|
46
|
-
<dd><a href="association_basics.html">Active Record Associations</a></dd>
|
47
|
-
<dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
|
48
|
-
<dt>Views</dt>
|
49
|
-
<dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
|
50
|
-
<dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
|
51
|
-
<dt>Controllers</dt>
|
52
|
-
<dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
|
53
|
-
<dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
|
54
|
-
</dl>
|
55
|
-
<dl class="R">
|
56
|
-
<dt>Digging Deeper</dt>
|
57
|
-
<dd><a href="i18n.html">Rails Internationalization API</a></dd>
|
58
|
-
<dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
|
59
|
-
<dd><a href="testing.html">Testing Rails Applications</a></dd>
|
60
|
-
<dd><a href="security.html">Securing Rails Applications</a></dd>
|
61
|
-
<dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
|
62
|
-
<dd><a href="performance_testing.html">Performance Testing Rails Applications</a></dd>
|
63
|
-
<dd><a href="plugins.html">The Basics of Creating Rails Plugins</a></dd>
|
64
|
-
<dd><a href="configuring.html">Configuring Rails Applications</a></dd>
|
65
|
-
<dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
|
66
|
-
<dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
|
67
|
-
<dd><a href="caching_with_rails.html">Caching with Rails</a></dd>
|
68
|
-
<dd><a href="contributing_to_rails.html">Contributing to Rails</a></dd>
|
69
|
-
</dl>
|
70
|
-
</div>
|
71
|
-
</li>
|
72
|
-
<li><a href="contribute.html">Contribute</a></li>
|
73
|
-
<li><a href="credits.html">Credits</a></li>
|
74
|
-
</ul>
|
75
|
-
</div>
|
76
|
-
</div>
|
77
|
-
<hr class="hide" />
|
78
|
-
|
79
|
-
<div id="feature">
|
80
|
-
<div class="wrapper">
|
81
|
-
<%= yield :header_section %>
|
82
|
-
|
83
|
-
<%= yield :index_section %>
|
84
|
-
</div>
|
85
|
-
</div>
|
86
|
-
|
87
|
-
<div id="container">
|
88
|
-
<div class="wrapper">
|
89
|
-
<div id="mainCol">
|
90
|
-
<%= yield %>
|
91
|
-
</div>
|
92
|
-
</div>
|
93
|
-
</div>
|
94
|
-
|
95
|
-
<hr class="hide" />
|
96
|
-
<div id="footer">
|
97
|
-
<div class="wrapper">
|
98
|
-
<p>This work is licensed under a <a href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-Share Alike 3.0</a> License</a></p>
|
99
|
-
<p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
|
100
|
-
</div>
|
101
|
-
</div>
|
102
|
-
</body>
|
103
|
-
</html>
|
@@ -1,979 +0,0 @@
|
|
1
|
-
h2. Layouts and Rendering in Rails
|
2
|
-
|
3
|
-
This guide covers the basic layout features of Action Controller and Action View. By referring to this guide, you will be able to:
|
4
|
-
|
5
|
-
* Use the various rendering methods built in to Rails
|
6
|
-
* Create layouts with multiple content sections
|
7
|
-
* Use partials to DRY up your views
|
8
|
-
* Use nested layouts (sub-templates)
|
9
|
-
|
10
|
-
endprologue.
|
11
|
-
|
12
|
-
h3. Overview: How the Pieces Fit Together
|
13
|
-
|
14
|
-
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.
|
15
|
-
|
16
|
-
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.
|
17
|
-
|
18
|
-
h3. Creating Responses
|
19
|
-
|
20
|
-
From the controller's point of view, there are three ways to create an HTTP response:
|
21
|
-
|
22
|
-
* Call +render+ to create a full response to send back to the browser
|
23
|
-
* Call +redirect_to+ to send an HTTP redirect status code to the browser
|
24
|
-
* Call +head+ to create a response consisting solely of HTTP headers to send back to the browser
|
25
|
-
|
26
|
-
I'll cover each of these methods in turn. But first, a few words about the very easiest thing that the controller can do to create a response: nothing at all.
|
27
|
-
|
28
|
-
h4. Rendering by Default: Convention Over Configuration in Action
|
29
|
-
|
30
|
-
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 actions. For example, if you have this code in your +BooksController+ class:
|
31
|
-
|
32
|
-
<ruby>
|
33
|
-
def show
|
34
|
-
@book = Book.find(params[:id])
|
35
|
-
end
|
36
|
-
</ruby>
|
37
|
-
|
38
|
-
Rails will automatically render +app/views/books/show.html.erb+ after running the method. In fact, if you have the default catch-all route in place (+map.connect ':controller/:action/:id'+), Rails will even render views that don't have any code at all in the controller. For example, if you have the default route in place and a request comes in for +/books/sale_list+, Rails will render +app/views/books/sale_list.html.erb+ in response.
|
39
|
-
|
40
|
-
NOTE: The actual rendering is done by subclasses of +ActionView::TemplateHandlers+. This guide does not dig into that process, but it's important to know that the file extension on your view controls the choice of template handler. In Rails 2, the standard extensions are +.erb+ for ERB (HTML with embedded Ruby), +.rjs+ for RJS (javascript with embedded ruby) and +.builder+ for Builder (XML generator). You'll also find +.rhtml+ used for ERB templates and +.rxml+ for Builder templates, but those extensions are now formally deprecated and will be removed from a future version of Rails.
|
41
|
-
|
42
|
-
h4. Using +render+
|
43
|
-
|
44
|
-
In most cases, the +ActionController::Base#render+ method does the heavy lifting of rendering your application's content for use by a browser. There are a variety of ways to customize the behavior of +render+. You can render the default view for a Rails template, or a specific template, or a file, or inline code, or nothing at all. You can render text, JSON, or XML. You can specify the content type or HTTP status of the rendered response as well.
|
45
|
-
|
46
|
-
TIP: If you want to see the exact results of a call to +render+ without needing to inspect it in a browser, you can call +render_to_string+. This method takes exactly the same options as +render+, but it returns a string instead of sending a response back to the browser.
|
47
|
-
|
48
|
-
h5. Rendering Nothing
|
49
|
-
|
50
|
-
Perhaps the simplest thing you can do with +render+ is to render nothing at all:
|
51
|
-
|
52
|
-
<ruby>
|
53
|
-
render :nothing => true
|
54
|
-
</ruby>
|
55
|
-
|
56
|
-
This will send an empty response to the browser (though it will include any status headers you set with the :status option, discussed below).
|
57
|
-
|
58
|
-
TIP: You should probably be using the +head+ method, discussed later in this guide, instead of +render :nothing+. This provides additional flexibility and makes it explicit that you're only generating HTTP headers.
|
59
|
-
|
60
|
-
h5. Rendering an Action's View
|
61
|
-
|
62
|
-
If you want to render the view that corresponds to a different action within the same template, you can use +render+ with the name of the view:
|
63
|
-
|
64
|
-
<ruby>
|
65
|
-
def update
|
66
|
-
@book = Book.find(params[:id])
|
67
|
-
if @book.update_attributes(params[:book])
|
68
|
-
redirect_to(@book)
|
69
|
-
else
|
70
|
-
render "edit"
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
</ruby>
|
75
|
-
|
76
|
-
If the call to +update_attributes+ fails, calling the +update+ action in this controller will render the +edit.html.erb+ template belonging to the same controller.
|
77
|
-
|
78
|
-
If you prefer, you can use a symbol instead of a string to specify the action to render:
|
79
|
-
|
80
|
-
<ruby>
|
81
|
-
def update
|
82
|
-
@book = Book.find(params[:id])
|
83
|
-
if @book.update_attributes(params[:book])
|
84
|
-
redirect_to(@book)
|
85
|
-
else
|
86
|
-
render :edit
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
</ruby>
|
91
|
-
|
92
|
-
To be explicit, you can use +render+ with the +:action+ option (though this is no longer necessary as of Rails 2.3):
|
93
|
-
|
94
|
-
<ruby>
|
95
|
-
def update
|
96
|
-
@book = Book.find(params[:id])
|
97
|
-
if @book.update_attributes(params[:book])
|
98
|
-
redirect_to(@book)
|
99
|
-
else
|
100
|
-
render :action => "edit"
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
</ruby>
|
105
|
-
|
106
|
-
WARNING: Using +render+ with +:action+ is a frequent source of confusion for Rails newcomers. The specified action is used to determine which view to render, but Rails does _not_ run any of the code for that action in the controller. Any instance variables that you require in the view must be set up in the current action before calling +render+.
|
107
|
-
|
108
|
-
h5. Rendering an Action's Template from Another Controller
|
109
|
-
|
110
|
-
What if you want to render a template from an entirely different controller from the one that contains the action code? You can also do that with +render+, which accepts the full path (relative to +app/views+) of the template to render. For example, if you're running code in an +AdminProductsController+ that lives in +app/controllers/admin+, you can render the results of an action to a template in +app/views/products+ this way:
|
111
|
-
|
112
|
-
<ruby>
|
113
|
-
render 'products/show'
|
114
|
-
</ruby>
|
115
|
-
|
116
|
-
Rails knows that this view belongs to a different controller because of the embedded slash character in the string. If you want to be explicit, you can use the +:template+ option (which was required on Rails 2.2 and earlier):
|
117
|
-
|
118
|
-
<ruby>
|
119
|
-
render :template => 'products/show'
|
120
|
-
</ruby>
|
121
|
-
|
122
|
-
h5. Rendering an Arbitrary File
|
123
|
-
|
124
|
-
The +render+ method can also use a view that's entirely outside of your application (perhaps you're sharing views between two Rails applications):
|
125
|
-
|
126
|
-
<ruby>
|
127
|
-
render "/u/apps/warehouse_app/current/app/views/products/show"
|
128
|
-
</ruby>
|
129
|
-
|
130
|
-
Rails determines that this is a file render because of the leading slash character. To be explicit, you can use the +:file+ option (which was required on Rails 2.2 and earlier):
|
131
|
-
|
132
|
-
<ruby>
|
133
|
-
render :file =>
|
134
|
-
"/u/apps/warehouse_app/current/app/views/products/show"
|
135
|
-
</ruby>
|
136
|
-
|
137
|
-
The +:file+ option takes an absolute file-system path. Of course, you need to have rights to the view that you're using to render the content.
|
138
|
-
|
139
|
-
NOTE: By default, the file is rendered without using the current layout. If you want Rails to put the file into the current layout, you need to add the +:layout => true+ option.
|
140
|
-
|
141
|
-
TIP: If you're running on Microsoft Windows, you should use the +:file+ option to render a file, because Windows filenames do not have the same format as Unix filenames.
|
142
|
-
|
143
|
-
h5. Using +render+ with +:inline+
|
144
|
-
|
145
|
-
The +render+ method can do without a view completely, if you're willing to use the +:inline+ option to supply ERB as part of the method call. This is perfectly valid:
|
146
|
-
|
147
|
-
<ruby>
|
148
|
-
render :inline =>
|
149
|
-
"<% products.each do |p| %><p><%= p.name %><p><% end %>"
|
150
|
-
</ruby>
|
151
|
-
|
152
|
-
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.
|
153
|
-
|
154
|
-
By default, inline rendering uses ERb. You can force it to use Builder instead with the +:type+ option:
|
155
|
-
|
156
|
-
<ruby>
|
157
|
-
render :inline =>
|
158
|
-
"xml.p {'Horrid coding practice!'}", :type => :builder
|
159
|
-
</ruby>
|
160
|
-
|
161
|
-
h5. Using +render+ with +:update+
|
162
|
-
|
163
|
-
You can also render javascript-based page updates inline using the +:update+ option to +render+:
|
164
|
-
|
165
|
-
<ruby>
|
166
|
-
render :update do |page|
|
167
|
-
page.replace_html 'warning', "Invalid options supplied"
|
168
|
-
end
|
169
|
-
</ruby>
|
170
|
-
|
171
|
-
WARNING: Placing javascript updates in your controller may seem to streamline small updates, but it defeats the MVC orientation of Rails and will make it harder for other developers to follow the logic of your project. We recommend using a separate rjs template instead, no matter how small the update.
|
172
|
-
|
173
|
-
h5. Rendering Text
|
174
|
-
|
175
|
-
You can send plain text - with no markup at all - back to the browser by using the +:text+ option to +render+:
|
176
|
-
|
177
|
-
<ruby>
|
178
|
-
render :text => "OK"
|
179
|
-
</ruby>
|
180
|
-
|
181
|
-
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.
|
182
|
-
|
183
|
-
NOTE: By default, if you use the +:text+ option, the file 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
|
184
|
-
|
185
|
-
h5. Rendering JSON
|
186
|
-
|
187
|
-
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:
|
188
|
-
|
189
|
-
<ruby>
|
190
|
-
render :json => @product
|
191
|
-
</ruby>
|
192
|
-
|
193
|
-
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.
|
194
|
-
|
195
|
-
h5. Rendering XML
|
196
|
-
|
197
|
-
Rails also has built-in support for converting objects to XML and rendering that XML back to the caller:
|
198
|
-
|
199
|
-
<ruby>
|
200
|
-
render :xml => @product
|
201
|
-
</ruby>
|
202
|
-
|
203
|
-
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.
|
204
|
-
|
205
|
-
h5. Rendering Vanilla JavaScript
|
206
|
-
|
207
|
-
Rails can render vanilla JavaScript (as an alternative to using +update+ with n +.rjs+ file):
|
208
|
-
|
209
|
-
<ruby>
|
210
|
-
render :js => "alert('Hello Rails');"
|
211
|
-
</ruby>
|
212
|
-
|
213
|
-
This will send the supplied string to the browser with a MIME type of +text/javascript+.
|
214
|
-
|
215
|
-
h5. Options for +render+
|
216
|
-
|
217
|
-
Calls to the +render+ method generally accept four options:
|
218
|
-
|
219
|
-
* +:content_type+
|
220
|
-
* +:layout+
|
221
|
-
* +:status+
|
222
|
-
* +:location+
|
223
|
-
|
224
|
-
h6. The +:content_type+ Option
|
225
|
-
|
226
|
-
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:
|
227
|
-
|
228
|
-
<ruby>
|
229
|
-
render :file => filename, :content_type => 'application/rss'
|
230
|
-
</ruby>
|
231
|
-
|
232
|
-
h6. The +:layout+ Option
|
233
|
-
|
234
|
-
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.
|
235
|
-
|
236
|
-
You can use the +:layout+ option to tell Rails to use a specific file as the layout for the current action:
|
237
|
-
|
238
|
-
<ruby>
|
239
|
-
render :layout => 'special_layout'
|
240
|
-
</ruby>
|
241
|
-
|
242
|
-
You can also tell Rails to render with no layout at all:
|
243
|
-
|
244
|
-
<ruby>
|
245
|
-
render :layout => false
|
246
|
-
</ruby>
|
247
|
-
|
248
|
-
h6. The +:status+ Option
|
249
|
-
|
250
|
-
Rails will automatically generate a response with the correct HTML status code (in most cases, this is +200 OK+). You can use the +:status+ option to change this:
|
251
|
-
|
252
|
-
<ruby>
|
253
|
-
render :status => 500
|
254
|
-
render :status => :forbidden
|
255
|
-
</ruby>
|
256
|
-
|
257
|
-
Rails understands either numeric status codes or symbols for status codes. You can find its list of status codes in +actionpack/lib/action_controller/status_codes.rb+. You can also see there how Rails maps symbols to status codes.
|
258
|
-
|
259
|
-
h6. The +:location+ Option
|
260
|
-
|
261
|
-
You can use the +:location+ option to set the HTTP +Location+ header:
|
262
|
-
|
263
|
-
<ruby>
|
264
|
-
render :xml => photo, :location => photo_url(photo)
|
265
|
-
</ruby>
|
266
|
-
|
267
|
-
h5. Finding Layouts
|
268
|
-
|
269
|
-
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+ ot +/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.
|
270
|
-
|
271
|
-
h6. Specifying Layouts on a per-Controller Basis
|
272
|
-
|
273
|
-
You can override the automatic layout conventions in your controllers by using the +layout+ declaration in the controller. For example:
|
274
|
-
|
275
|
-
<ruby>
|
276
|
-
class ProductsController < ApplicationController
|
277
|
-
layout "inventory"
|
278
|
-
#...
|
279
|
-
end
|
280
|
-
</ruby>
|
281
|
-
|
282
|
-
With this declaration, all methods within +ProductsController+ will use +app/views/layouts/inventory.html.erb+ for their layout.
|
283
|
-
|
284
|
-
To assign a specific layout for the entire application, use a declaration in your +ApplicationController+ class:
|
285
|
-
|
286
|
-
<ruby>
|
287
|
-
class ApplicationController < ActionController::Base
|
288
|
-
layout "main"
|
289
|
-
#...
|
290
|
-
end
|
291
|
-
</ruby>
|
292
|
-
|
293
|
-
With this declaration, all views in the entire application will use +app/views/layouts/main.html.erb+ for their layout.
|
294
|
-
|
295
|
-
h6. Choosing Layouts at Runtime
|
296
|
-
|
297
|
-
You can use a symbol to defer the choice of layout until a request is processed:
|
298
|
-
|
299
|
-
<ruby>
|
300
|
-
class ProductsController < ApplicationController
|
301
|
-
layout :products_layout
|
302
|
-
|
303
|
-
def show
|
304
|
-
@product = Product.find(params[:id])
|
305
|
-
end
|
306
|
-
|
307
|
-
private
|
308
|
-
def products_layout
|
309
|
-
@current_user.special? ? "special" : "products"
|
310
|
-
end
|
311
|
-
|
312
|
-
end
|
313
|
-
</ruby>
|
314
|
-
|
315
|
-
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 to determine the layout:
|
316
|
-
|
317
|
-
<ruby>
|
318
|
-
class ProductsController < ApplicationController
|
319
|
-
layout proc { |controller| controller.request.xhr? ? 'popup' : 'application' }
|
320
|
-
# ...
|
321
|
-
end
|
322
|
-
</ruby>
|
323
|
-
|
324
|
-
h6. Conditional Layouts
|
325
|
-
|
326
|
-
Layouts specified at the controller level support +:only+ and +:except+ options that take either a method name or an array of method names:
|
327
|
-
|
328
|
-
<ruby>
|
329
|
-
class ProductsController < ApplicationController
|
330
|
-
layout "product", :except => [:index, :rss]
|
331
|
-
#...
|
332
|
-
end
|
333
|
-
</ruby>
|
334
|
-
|
335
|
-
With this declaration, the +product+ layout would be used for everything but the +rss+ and +index+ methods.
|
336
|
-
|
337
|
-
h6. Layout Inheritance
|
338
|
-
|
339
|
-
Layouts are shared downwards in the hierarchy, and more specific layouts always override more general ones. For example:
|
340
|
-
|
341
|
-
* +application_controller.rb+
|
342
|
-
|
343
|
-
<ruby>
|
344
|
-
class ApplicationController < ActionController::Base
|
345
|
-
layout "main"
|
346
|
-
#...
|
347
|
-
end
|
348
|
-
</ruby>
|
349
|
-
|
350
|
-
* +posts_controller.rb+
|
351
|
-
|
352
|
-
<ruby>
|
353
|
-
class PostsController < ApplicationController
|
354
|
-
# ...
|
355
|
-
end
|
356
|
-
</ruby>
|
357
|
-
|
358
|
-
* +special_posts_controller.rb+
|
359
|
-
|
360
|
-
<ruby>
|
361
|
-
class SpecialPostsController < PostsController
|
362
|
-
layout "special"
|
363
|
-
# ...
|
364
|
-
end
|
365
|
-
</ruby>
|
366
|
-
|
367
|
-
* +old_posts_controller.rb+
|
368
|
-
|
369
|
-
<ruby>
|
370
|
-
class OldPostsController < SpecialPostsController
|
371
|
-
layout nil
|
372
|
-
|
373
|
-
def show
|
374
|
-
@post = Post.find(params[:id])
|
375
|
-
end
|
376
|
-
|
377
|
-
def index
|
378
|
-
@old_posts = Post.older
|
379
|
-
render :layout => "old"
|
380
|
-
end
|
381
|
-
# ...
|
382
|
-
end
|
383
|
-
</ruby>
|
384
|
-
|
385
|
-
In this application:
|
386
|
-
|
387
|
-
* In general, views will be rendered in the +main+ layout
|
388
|
-
* +PostsController#index+ will use the +main+ layout
|
389
|
-
* +SpecialPostsController#index+ will use the +special+ layout
|
390
|
-
* +OldPostsController#show+ will use no layout at all
|
391
|
-
* +OldPostsController#index+ will use the +old+ layout
|
392
|
-
|
393
|
-
h5. Avoiding Double Render Errors
|
394
|
-
|
395
|
-
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.
|
396
|
-
|
397
|
-
For example, here's some code that will trigger this error:
|
398
|
-
|
399
|
-
<ruby>
|
400
|
-
def show
|
401
|
-
@book = Book.find(params[:id])
|
402
|
-
if @book.special?
|
403
|
-
render :action => "special_show"
|
404
|
-
end
|
405
|
-
render :action => "regular_show"
|
406
|
-
end
|
407
|
-
</ruby>
|
408
|
-
|
409
|
-
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 +show+ view - and throw an error. The solution is simple: make sure that you only have 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:
|
410
|
-
|
411
|
-
<ruby>
|
412
|
-
def show
|
413
|
-
@book = Book.find(params[:id])
|
414
|
-
if @book.special?
|
415
|
-
render :action => "special_show" and return
|
416
|
-
end
|
417
|
-
render :action => "regular_show"
|
418
|
-
end
|
419
|
-
</ruby>
|
420
|
-
|
421
|
-
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:
|
422
|
-
|
423
|
-
<ruby>
|
424
|
-
def show
|
425
|
-
@book = Book.find(params[:id])
|
426
|
-
if @book.special?
|
427
|
-
render :action => "special_show"
|
428
|
-
end
|
429
|
-
end
|
430
|
-
</ruby>
|
431
|
-
|
432
|
-
This will render a book with +special?+ set with the +special_show+ template, while other books will render with the default +show+ template.
|
433
|
-
|
434
|
-
h4. Using +redirect_to+
|
435
|
-
|
436
|
-
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:
|
437
|
-
|
438
|
-
<ruby>
|
439
|
-
redirect_to photos_path
|
440
|
-
</ruby>
|
441
|
-
|
442
|
-
You can use +redirect_to+ with any arguments that you could use with +link_to+ or +url_for+. In addition, there's a special redirect that sends the user back to the page they just came from:
|
443
|
-
|
444
|
-
<ruby>
|
445
|
-
redirect_to :back
|
446
|
-
</ruby>
|
447
|
-
|
448
|
-
h5. Getting a Different Redirect Status Code
|
449
|
-
|
450
|
-
Rails uses HTTP status code 302 (permanent redirect) when you call +redirect_to+. If you'd like to use a different status code (perhaps 301, temporary redirect), you can do so by using the +:status+ option:
|
451
|
-
|
452
|
-
<ruby>
|
453
|
-
redirect_to photos_path, :status => 301
|
454
|
-
</ruby>
|
455
|
-
|
456
|
-
Just like the +:status+ option for +render+, +:status+ for +redirect_to+ accepts both numeric and symbolic header designations.
|
457
|
-
|
458
|
-
h5. The Difference Between +render+ and +redirect_to+
|
459
|
-
|
460
|
-
Sometimes inexperienced developers conceive 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.
|
461
|
-
|
462
|
-
Consider these actions to see the difference:
|
463
|
-
|
464
|
-
<ruby>
|
465
|
-
def index
|
466
|
-
@books = Book.find(:all)
|
467
|
-
end
|
468
|
-
|
469
|
-
def show
|
470
|
-
@book = Book.find(params[:id])
|
471
|
-
if @book.nil?
|
472
|
-
render :action => "index"
|
473
|
-
end
|
474
|
-
end
|
475
|
-
</ruby>
|
476
|
-
|
477
|
-
With the code in this form, there will be 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 is presumably depending on. One way to fix this is to redirect instead of rendering:
|
478
|
-
|
479
|
-
<ruby>
|
480
|
-
def index
|
481
|
-
@books = Book.find(:all)
|
482
|
-
end
|
483
|
-
|
484
|
-
def show
|
485
|
-
@book = Book.find(params[:id])
|
486
|
-
if @book.nil?
|
487
|
-
redirect_to :action => "index"
|
488
|
-
end
|
489
|
-
end
|
490
|
-
</ruby>
|
491
|
-
|
492
|
-
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.
|
493
|
-
|
494
|
-
h4. Using +head+ To Build Header-Only Responses
|
495
|
-
|
496
|
-
The +head+ method exists to let you send back responses to the browser that have only headers. It provides a more obvious alternative to calling +render :nothing+. The +head+ method takes one response, which is interpreted as a hash of header names and values. For example, you can return only an error header:
|
497
|
-
|
498
|
-
<ruby>
|
499
|
-
head :bad_request
|
500
|
-
</ruby>
|
501
|
-
|
502
|
-
Or you can use other HTTP headers to convey additional information:
|
503
|
-
|
504
|
-
<ruby>
|
505
|
-
head :created, :location => photo_path(@photo)
|
506
|
-
</ruby>
|
507
|
-
|
508
|
-
h3. Structuring Layouts
|
509
|
-
|
510
|
-
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:
|
511
|
-
|
512
|
-
* Asset tags
|
513
|
-
* +yield+ and +content_for+
|
514
|
-
* Partials
|
515
|
-
|
516
|
-
I'll discuss each of these in turn.
|
517
|
-
|
518
|
-
h4. Asset Tags
|
519
|
-
|
520
|
-
Asset tags provide methods for generating HTML that links views to assets like images, javascript, stylesheets, and feeds. There are four types of include tag:
|
521
|
-
|
522
|
-
* auto_discovery_link_tag
|
523
|
-
* javascript_include_tag
|
524
|
-
* stylesheet_link_tag
|
525
|
-
* image_tag
|
526
|
-
|
527
|
-
You can use these tags in layouts or other views, although the tags other than +image_tag+ are most commonly used in the +<head>+ section of a layout.
|
528
|
-
|
529
|
-
WARNING: The asset tags 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.
|
530
|
-
|
531
|
-
h5. Linking to Feeds with +auto_discovery_link_tag+
|
532
|
-
|
533
|
-
The +auto_discovery_link_tag+ helper builds HTML that most browsers and newsreaders can use to detect the presences 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:
|
534
|
-
|
535
|
-
<erb>
|
536
|
-
<%= auto_discovery_link_tag(:rss, {:action => "feed"},
|
537
|
-
{:title => "RSS Feed"}) %>
|
538
|
-
</erb>
|
539
|
-
|
540
|
-
There are three tag options available for +auto_discovery_link_tag+:
|
541
|
-
|
542
|
-
* +:rel+ specifies the +rel+ value in the link (defaults to "alternate")
|
543
|
-
* +:type+ specifies an explicit MIME type. Rails will generate an appropriate MIME type automatically
|
544
|
-
* +:title+ specifies the title of the link
|
545
|
-
|
546
|
-
h5. Linking to Javascript Files with +javascript_include_tag+
|
547
|
-
|
548
|
-
The +javascript_include_tag+ helper returns an HTML +script+ tag for each source provided. Rails looks in +public/javascripts+ for these files by default, but you can specify a full path relative to the document root, or a URL, if you prefer. For example, to include +public/javascripts/main.js+:
|
549
|
-
|
550
|
-
<erb>
|
551
|
-
<%= javascript_include_tag "main" %>
|
552
|
-
</erb>
|
553
|
-
|
554
|
-
To include +public/javascripts/main.js+ and +public/javascripts/columns.js+:
|
555
|
-
|
556
|
-
<erb>
|
557
|
-
<%= javascript_include_tag "main", "columns" %>
|
558
|
-
</erb>
|
559
|
-
|
560
|
-
To include +public/javascripts/main.js+ and +public/photos/columns.js+:
|
561
|
-
|
562
|
-
<erb>
|
563
|
-
<%= javascript_include_tag "main", "/photos/columns" %>
|
564
|
-
</erb>
|
565
|
-
|
566
|
-
To include +http://example.com/main.js+:
|
567
|
-
|
568
|
-
<erb>
|
569
|
-
<%= javascript_include_tag "http://example.com/main.js" %>
|
570
|
-
</erb>
|
571
|
-
|
572
|
-
The +defaults+ option loads the Prototype and Scriptaculous libraries:
|
573
|
-
|
574
|
-
<erb>
|
575
|
-
<%= javascript_include_tag :defaults %>
|
576
|
-
</erb>
|
577
|
-
|
578
|
-
The +all+ option loads every javascript file in +public/javascripts+, starting with the Prototype and Scriptaculous libraries:
|
579
|
-
|
580
|
-
<erb>
|
581
|
-
<%= javascript_include_tag :all %>
|
582
|
-
</erb>
|
583
|
-
|
584
|
-
You can supply the +:recursive+ option to load files in subfolders of +public/javascripts+ as well:
|
585
|
-
|
586
|
-
<erb>
|
587
|
-
<%= javascript_include_tag :all, :recursive => true %>
|
588
|
-
</erb>
|
589
|
-
|
590
|
-
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+:
|
591
|
-
|
592
|
-
<erb>
|
593
|
-
<%= javascript_include_tag "main", "columns", :cache => true %>
|
594
|
-
</erb>
|
595
|
-
|
596
|
-
By default, the combined file will be delivered as +javascripts/all.js+. You can specify a location for the cached asset file instead:
|
597
|
-
|
598
|
-
<erb>
|
599
|
-
<%= javascript_include_tag "main", "columns",
|
600
|
-
:cache => 'cache/main/display' %>
|
601
|
-
</erb>
|
602
|
-
|
603
|
-
You can even use dynamic paths such as +cache/#{current_site}/main/display+.
|
604
|
-
|
605
|
-
h5. Linking to CSS Files with +stylesheet_link_tag+
|
606
|
-
|
607
|
-
The +stylesheet_link_tag+ helper returns an HTML +<link>+ tag for each source provided. Rails looks in +public/stylesheets+ for these files by default, but you can specify a full path relative to the document root, or a URL, if you prefer. For example, to include +public/stylesheets/main.cs+:
|
608
|
-
|
609
|
-
<erb>
|
610
|
-
<%= stylesheet_link_tag "main" %>
|
611
|
-
</erb>
|
612
|
-
|
613
|
-
To include +public/stylesheets/main.css+ and +public/stylesheets/columns.css+:
|
614
|
-
|
615
|
-
<erb>
|
616
|
-
<%= stylesheet_link_tag "main", "columns" %>
|
617
|
-
</erb>
|
618
|
-
|
619
|
-
To include +public/stylesheets/main.css+ and +public/photos/columns.css+:
|
620
|
-
|
621
|
-
<erb>
|
622
|
-
<%= stylesheet_link_tag "main", "/photos/columns" %>
|
623
|
-
</erb>
|
624
|
-
|
625
|
-
To include +http://example.com/main.cs+:
|
626
|
-
|
627
|
-
<erb>
|
628
|
-
<%= stylesheet_link_tag "http://example.com/main.cs" %>
|
629
|
-
</erb>
|
630
|
-
|
631
|
-
By default, +stylesheet_link_tag+ creates links with +media="screen" rel="stylesheet" type="text/css"+. You can override any of these defaults by specifying an appropriate option (:media, :rel, or :type):
|
632
|
-
|
633
|
-
<erb>
|
634
|
-
<%= stylesheet_link_tag "main_print", media => "print" %>
|
635
|
-
</erb>
|
636
|
-
|
637
|
-
The +all+ option links every CSS file in +public/stylesheets+:
|
638
|
-
|
639
|
-
<erb>
|
640
|
-
<%= stylesheet_link_tag :all %>
|
641
|
-
</erb>
|
642
|
-
|
643
|
-
You can supply the +:recursive+ option to link files in subfolders of +public/stylesheets+ as well:
|
644
|
-
|
645
|
-
<erb>
|
646
|
-
<%= stylesheet_link_tag :all, :recursive => true %>
|
647
|
-
</erb>
|
648
|
-
|
649
|
-
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+:
|
650
|
-
|
651
|
-
<erb>
|
652
|
-
<%= stylesheet_link_tag "main", "columns", :cache => true %>
|
653
|
-
</erb>
|
654
|
-
|
655
|
-
By default, the combined file will be delivered as +stylesheets/all.css+. You can specify a location for the cached asset file instead:
|
656
|
-
|
657
|
-
<erb>
|
658
|
-
<%= stylesheet_link_tag "main", "columns",
|
659
|
-
:cache => 'cache/main/display' %>
|
660
|
-
</erb>
|
661
|
-
|
662
|
-
You can even use dynamic paths such as +cache/#{current_site}/main/display+.
|
663
|
-
|
664
|
-
h5. Linking to Images with +image_tag+
|
665
|
-
|
666
|
-
The +image_tag+ helper builds an HTML +<image>+ tag to the specified file. By default, files are loaded from +public/images+. If you don't specify an extension, +.png+ is assumed by default:
|
667
|
-
|
668
|
-
<erb>
|
669
|
-
<%= image_tag "header" %>
|
670
|
-
</erb>
|
671
|
-
|
672
|
-
You can supply a path to the image if you like:
|
673
|
-
|
674
|
-
<erb>
|
675
|
-
<%= image_tag "icons/delete.gif" %>
|
676
|
-
</erb>
|
677
|
-
|
678
|
-
You can supply a hash of additional HTML options:
|
679
|
-
|
680
|
-
<erb>
|
681
|
-
<%= image_tag "icons/delete.gif", :height => 45 %>
|
682
|
-
</erb>
|
683
|
-
|
684
|
-
There are also three special options you can use with +image_tag+:
|
685
|
-
|
686
|
-
* +:alt+ specifies the alt text for the image (which defaults to the file name of the file, capitalized and with no extension)
|
687
|
-
* +:size+ specifies both width and height, in the format "{width}x{height}" (for example, "150x125")
|
688
|
-
* +:mouseover+ sets an alternate image to be used when the onmouseover event is fired.
|
689
|
-
|
690
|
-
h4. Understanding +yield+
|
691
|
-
|
692
|
-
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:
|
693
|
-
|
694
|
-
<erb>
|
695
|
-
<html>
|
696
|
-
<head>
|
697
|
-
</head>
|
698
|
-
<body>
|
699
|
-
<%= yield %>
|
700
|
-
</body>
|
701
|
-
</html>
|
702
|
-
</erb>
|
703
|
-
|
704
|
-
You can also create a layout with multiple yielding regions:
|
705
|
-
|
706
|
-
<erb>
|
707
|
-
<html>
|
708
|
-
<head>
|
709
|
-
<%= yield :head %>
|
710
|
-
</head>
|
711
|
-
<body>
|
712
|
-
<%= yield %>
|
713
|
-
</body>
|
714
|
-
</html>
|
715
|
-
</erb>
|
716
|
-
|
717
|
-
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.
|
718
|
-
|
719
|
-
h4. Using +content_for+
|
720
|
-
|
721
|
-
The +content_for+ method allows you to insert content into a +yield+ block in your layout. You only use +content_for+ to insert content in named yields. For example, this view would work with the layout that you just saw:
|
722
|
-
|
723
|
-
<erb>
|
724
|
-
<% content_for :head do %>
|
725
|
-
<title>A simple page</title>
|
726
|
-
<% end %>
|
727
|
-
|
728
|
-
<p>Hello, Rails!</p>
|
729
|
-
</erb>
|
730
|
-
|
731
|
-
The result of rendering this page into the supplied layout would be this HTML:
|
732
|
-
|
733
|
-
<erb>
|
734
|
-
<html>
|
735
|
-
<head>
|
736
|
-
<title>A simple page</title>
|
737
|
-
</head>
|
738
|
-
<body>
|
739
|
-
<p>Hello, Rails!</p>
|
740
|
-
</body>
|
741
|
-
</html>
|
742
|
-
</erb>
|
743
|
-
|
744
|
-
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.
|
745
|
-
|
746
|
-
h4. Using Partials
|
747
|
-
|
748
|
-
Partial templates - usually just called "partials" - are another device for breaking apart 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.
|
749
|
-
|
750
|
-
h5. Naming Partials
|
751
|
-
|
752
|
-
To render a partial as part of a view, you use the +render+ method within the view, and include the +:partial+ option:
|
753
|
-
|
754
|
-
<ruby>
|
755
|
-
<%= render :partial => "menu" %>
|
756
|
-
</ruby>
|
757
|
-
|
758
|
-
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:
|
759
|
-
|
760
|
-
<ruby>
|
761
|
-
<%= render :partial => "shared/menu" %>
|
762
|
-
</ruby>
|
763
|
-
|
764
|
-
That code will pull in the partial from +app/views/shared/_menu.html.erb+.
|
765
|
-
|
766
|
-
h5. Using Partials to Simplify Views
|
767
|
-
|
768
|
-
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:
|
769
|
-
|
770
|
-
<erb>
|
771
|
-
<%= render :partial => "shared/ad_banner" %>
|
772
|
-
|
773
|
-
<h1>Products</h1>
|
774
|
-
|
775
|
-
<p>Here are a few of our fine products:</p>
|
776
|
-
...
|
777
|
-
|
778
|
-
<%= render :partial => "shared/footer" %>
|
779
|
-
</erb>
|
780
|
-
|
781
|
-
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.
|
782
|
-
|
783
|
-
TIP: For content that is shared among all pages in your application, you can use partials directly from layouts.
|
784
|
-
|
785
|
-
h5. Partial Layouts
|
786
|
-
|
787
|
-
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:
|
788
|
-
|
789
|
-
<erb>
|
790
|
-
<%= render :partial => "link_area", :layout => "graybar" %>
|
791
|
-
</erb>
|
792
|
-
|
793
|
-
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).
|
794
|
-
|
795
|
-
h5. Passing Local Variables
|
796
|
-
|
797
|
-
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:
|
798
|
-
|
799
|
-
* +new.html.erb+
|
800
|
-
|
801
|
-
<erb>
|
802
|
-
<h1>New zone</h1>
|
803
|
-
<%= error_messages_for :zone %>
|
804
|
-
<%= render :partial => "form", :locals =>
|
805
|
-
{ :button_label => "Create zone", :zone => @zone } %>
|
806
|
-
</erb>
|
807
|
-
|
808
|
-
* +edit.html.erb+
|
809
|
-
|
810
|
-
<erb>
|
811
|
-
<h1>Editing zone</h1>
|
812
|
-
<%= error_messages_for :zone %>
|
813
|
-
<%= render :partial => "form", :locals =>
|
814
|
-
{ :button_label => "Update zone", :zone => @zone } %>
|
815
|
-
</erb>
|
816
|
-
|
817
|
-
* +_form.html.erb+
|
818
|
-
|
819
|
-
<erb>
|
820
|
-
<% form_for(zone) do |f| %>
|
821
|
-
<p>
|
822
|
-
<b>Zone name</b><br />
|
823
|
-
<%= f.text_field :name %>
|
824
|
-
</p>
|
825
|
-
<p>
|
826
|
-
<%= f.submit button_label %>
|
827
|
-
</p>
|
828
|
-
<% end %>
|
829
|
-
</erb>
|
830
|
-
|
831
|
-
Although the same partial will be rendered into both views, the label on the submit button is controlled by a local variable passed into the partial.
|
832
|
-
|
833
|
-
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:
|
834
|
-
|
835
|
-
<erb>
|
836
|
-
<%= render :partial => "customer", :object => @new_customer %>
|
837
|
-
</erb>
|
838
|
-
|
839
|
-
Within the +customer+ partial, the +customer+ variable will refer to +@new_customer+ from the parent view.
|
840
|
-
|
841
|
-
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 is deprecated in Rails 2.2 and will be removed in a future version.
|
842
|
-
|
843
|
-
If you have an instance of a model to render into a partial, you can use a shorthand syntax:
|
844
|
-
|
845
|
-
<erb>
|
846
|
-
<%= render :partial => @customer %>
|
847
|
-
</erb>
|
848
|
-
|
849
|
-
Assuming that the +@customer+ instance variable contains an instance of the +Customer+ model, this will use +_customer.html.erb+ to render it.
|
850
|
-
|
851
|
-
h5. Rendering Collections
|
852
|
-
|
853
|
-
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:
|
854
|
-
|
855
|
-
* +index.html.erb+
|
856
|
-
|
857
|
-
<erb>
|
858
|
-
<h1>Products</h1>
|
859
|
-
<%= render :partial => "product", :collection => @products %>
|
860
|
-
</erb>
|
861
|
-
|
862
|
-
* +_product.html.erb+
|
863
|
-
|
864
|
-
<erb>
|
865
|
-
<p>Product Name: <%= product.name %></p>
|
866
|
-
</erb>
|
867
|
-
|
868
|
-
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. To use a custom local variable name within the partial, specify the +:as+ option in the call to the partial:
|
869
|
-
|
870
|
-
<erb>
|
871
|
-
<%= render :partial => "product", :collection => @products, :as => :item %>
|
872
|
-
</erb>
|
873
|
-
|
874
|
-
With this change, you can access an instance of the +@products+ collection as the +item+ local variable within the partial.
|
875
|
-
|
876
|
-
TIP: Rails also makes a counter variable available within a partial called by the collection, named after the member of the collection followed by +_counter+. For example, if you're rendering +@products+, within the partial you can refer to +product_counter+ to tell you how many times the partial has been rendered.
|
877
|
-
|
878
|
-
You can also specify a second partial to be rendered between instances of the main partial by using the +:spacer_template+ option:
|
879
|
-
|
880
|
-
<erb>
|
881
|
-
<%= render :partial => "product", :collection => @products,
|
882
|
-
:spacer_template => "product_ruler" %>
|
883
|
-
</erb>
|
884
|
-
|
885
|
-
Rails will render the +_product_ruler+ partial (with no data passed in to it) between each pair of +_product+ partials.
|
886
|
-
|
887
|
-
There's also a shorthand syntax available for rendering collections. For example, if +@products+ is a collection of products, you can render the collection this way:
|
888
|
-
|
889
|
-
* +index.html.erb+
|
890
|
-
|
891
|
-
<erb>
|
892
|
-
<h1>Products</h1>
|
893
|
-
<%= render :partial => @products %>
|
894
|
-
</erb>
|
895
|
-
|
896
|
-
* +_product.html.erb+
|
897
|
-
|
898
|
-
<erb>
|
899
|
-
<p>Product Name: <%= product.name %></p>
|
900
|
-
</erb>
|
901
|
-
|
902
|
-
Rails determines the name of the partial to use by looking at the model name in the collection. In fact, you can even create a heterogeneous collection and render it this way, and Rails will choose the proper partial for each member of the collection:
|
903
|
-
|
904
|
-
* +index.html.erb+
|
905
|
-
|
906
|
-
<erb>
|
907
|
-
<h1>Contacts</h1>
|
908
|
-
<%= render :partial =>
|
909
|
-
[customer1, employee1, customer2, employee2] %>
|
910
|
-
</erb>
|
911
|
-
|
912
|
-
* +_customer.html.erb+
|
913
|
-
|
914
|
-
<erb>
|
915
|
-
<p>Name: <%= customer.name %></p>
|
916
|
-
</erb>
|
917
|
-
|
918
|
-
* +_employee.html.erb+
|
919
|
-
|
920
|
-
<erb>
|
921
|
-
<p>Name: <%= employee.name %></p>
|
922
|
-
</erb>
|
923
|
-
|
924
|
-
In this case, Rails will use the customer or employee partials as appropriate for each member of the collection.
|
925
|
-
|
926
|
-
h4. Using Nested Layouts
|
927
|
-
|
928
|
-
You may find that your application requires a layout that differs slightly from your regular application layout to support one particular controller. Rather than repeating the main layout and editing it, you can accomplish this by using nested layouts (sometimes called sub-templates). Here's an example:
|
929
|
-
|
930
|
-
Suppose you have the follow +ApplicationController+ layout:
|
931
|
-
|
932
|
-
* +app/views/layouts/application.html.erb+
|
933
|
-
|
934
|
-
<erb>
|
935
|
-
<html>
|
936
|
-
<head>
|
937
|
-
<title><%= @page_title or 'Page Title' %></title>
|
938
|
-
<%= stylesheet_link_tag 'layout' %>
|
939
|
-
<style type="text/css"><%= yield :stylesheets %></style>
|
940
|
-
</head>
|
941
|
-
<body>
|
942
|
-
<div id="top_menu">Top menu items here</div>
|
943
|
-
<div id="menu">Menu items here</div>
|
944
|
-
<div id="content"><%= yield(:content) or yield %></div>
|
945
|
-
</body>
|
946
|
-
</html>
|
947
|
-
</erb>
|
948
|
-
|
949
|
-
On pages generated by +NewsController+, you want to hide the top menu and add a right menu:
|
950
|
-
|
951
|
-
* +app/views/layouts/news.html.erb+
|
952
|
-
|
953
|
-
<erb>
|
954
|
-
<% content_for :stylesheets do %>
|
955
|
-
#top_menu {display: none}
|
956
|
-
#right_menu {float: right; background-color: yellow; color: black}
|
957
|
-
<% end -%>
|
958
|
-
<% content_for :content do %>
|
959
|
-
<div id="right_menu">Right menu items here</div>
|
960
|
-
<%= yield(:news_content) or yield %>
|
961
|
-
<% end -%>
|
962
|
-
<% render :file => 'layouts/application' %>
|
963
|
-
</erb>
|
964
|
-
|
965
|
-
That's it. The News views will use the new layout, hiding the top menu and adding a new right menu inside the "content" div.
|
966
|
-
|
967
|
-
There are several ways of getting similar results with different sub-templating schemes using this technique. Note that there is no limit in nesting levels. One can use the +ActionView::render+ method via +render :file => 'layouts/news'+ to base a new layout on the News layout. If one is sure she will not subtemplate the +News+ layout, she can ommit the +yield(:news_content) or + part.
|
968
|
-
|
969
|
-
h3. Changelog
|
970
|
-
|
971
|
-
"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/15
|
972
|
-
|
973
|
-
* December 27, 2008: Merge patch from Rodrigo Rosenfeld Rosas covering subtemplates
|
974
|
-
* December 27, 2008: Information on new rendering defaults by "Mike Gunderloy":credits.html#mgunderloy
|
975
|
-
* November 9, 2008: Added partial collection counter by "Mike Gunderloy":credits.html#mgunderloy
|
976
|
-
* November 1, 2008: Added +:js+ option for +render+ by "Mike Gunderloy":credits.html#mgunderloy
|
977
|
-
* October 16, 2008: Ready for publication by "Mike Gunderloy":credits.html#mgunderloy
|
978
|
-
* October 4, 2008: Additional info on partials (+:object+, +:as+, and +:spacer_template+) by "Mike Gunderloy":credits.html#mgunderloy (not yet approved for publication)
|
979
|
-
* September 28, 2008: First draft by "Mike Gunderloy":credits.html#mgunderloy (not yet approved for publication)
|