railties 3.0.0.beta → 3.0.0.beta2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +15 -1
- data/README +9 -9
- data/bin/rails +28 -9
- data/guides/images/challenge.png +0 -0
- data/guides/images/edge_badge.png +0 -0
- data/guides/images/posts_index.png +0 -0
- data/guides/images/rails_welcome.png +0 -0
- data/guides/rails_guides.rb +9 -22
- data/guides/rails_guides/generator.rb +79 -50
- data/guides/rails_guides/textile_extensions.rb +3 -3
- data/guides/source/2_2_release_notes.textile +1 -1
- data/guides/source/2_3_release_notes.textile +1 -1
- data/guides/source/3_0_release_notes.textile +46 -38
- data/guides/source/action_controller_overview.textile +2 -2
- data/guides/source/action_mailer_basics.textile +4 -4
- data/guides/source/action_view_overview.textile +2 -2
- data/guides/source/active_record_querying.textile +73 -95
- data/guides/source/active_support_core_extensions.textile +993 -85
- data/guides/source/activerecord_validations_callbacks.textile +3 -3
- data/guides/source/caching_with_rails.textile +1 -1
- data/guides/source/command_line.textile +90 -88
- data/guides/source/configuring.textile +10 -10
- data/guides/source/contribute.textile +2 -2
- data/guides/source/contributing_to_rails.textile +14 -7
- data/guides/source/credits.textile.erb +8 -0
- data/guides/source/debugging_rails_applications.textile +6 -6
- data/guides/source/form_helpers.textile +1 -1
- data/guides/source/generators.textile +14 -14
- data/guides/source/getting_started.textile +634 -500
- data/guides/source/index.textile.erb +16 -1
- data/guides/source/layout.html.erb +7 -1
- data/guides/source/layouts_and_rendering.textile +299 -71
- data/guides/source/migrations.textile +5 -5
- data/guides/source/performance_testing.textile +8 -8
- data/guides/source/plugins.textile +26 -24
- data/guides/source/rails_on_rack.textile +5 -5
- data/guides/source/routing.textile +119 -117
- data/guides/source/security.textile +1 -1
- data/guides/source/testing.textile +4 -4
- data/lib/rails.rb +4 -1
- data/lib/rails/application.rb +44 -7
- data/lib/rails/application/bootstrap.rb +2 -14
- data/lib/rails/application/configuration.rb +69 -5
- data/lib/rails/application/finisher.rb +2 -3
- data/lib/rails/application/metal_loader.rb +1 -1
- data/lib/rails/application/routes_reloader.rb +1 -1
- data/lib/rails/backtrace_cleaner.rb +0 -11
- data/lib/rails/commands.rb +7 -6
- data/lib/rails/commands/application.rb +1 -1
- data/lib/rails/commands/console.rb +1 -1
- data/lib/rails/commands/dbconsole.rb +12 -0
- data/lib/rails/commands/destroy.rb +2 -2
- data/lib/rails/commands/generate.rb +2 -2
- data/lib/rails/commands/performance/benchmarker.rb +2 -2
- data/lib/rails/commands/performance/profiler.rb +2 -2
- data/lib/rails/commands/plugin.rb +6 -6
- data/lib/rails/commands/runner.rb +2 -0
- data/lib/rails/commands/server.rb +23 -8
- data/lib/rails/configuration.rb +2 -84
- data/lib/rails/console/app.rb +4 -3
- data/lib/rails/console/helpers.rb +3 -1
- data/lib/rails/engine.rb +107 -12
- data/lib/rails/engine/configuration.rb +8 -2
- data/lib/rails/generators.rb +22 -7
- data/lib/rails/generators/actions.rb +16 -6
- data/lib/rails/generators/base.rb +15 -8
- data/lib/rails/generators/erb.rb +21 -0
- data/lib/{generators → rails/generators}/erb/controller/controller_generator.rb +4 -5
- data/lib/{generators → rails/generators}/erb/controller/templates/view.html.erb +0 -0
- data/lib/rails/generators/erb/mailer/mailer_generator.rb +13 -0
- data/lib/{generators → rails/generators}/erb/mailer/templates/view.text.erb +0 -0
- data/lib/{generators → rails/generators}/erb/scaffold/scaffold_generator.rb +14 -26
- data/lib/{generators → rails/generators}/erb/scaffold/templates/_form.html.erb +1 -1
- data/lib/{generators → rails/generators}/erb/scaffold/templates/edit.html.erb +0 -0
- data/lib/{generators → rails/generators}/erb/scaffold/templates/index.html.erb +1 -1
- data/lib/{generators → rails/generators}/erb/scaffold/templates/layout.html.erb +1 -0
- data/lib/{generators → rails/generators}/erb/scaffold/templates/new.html.erb +0 -0
- data/lib/{generators → rails/generators}/erb/scaffold/templates/show.html.erb +0 -0
- data/lib/rails/generators/named_base.rb +4 -0
- data/lib/{generators → rails/generators}/rails/app/USAGE +0 -0
- data/lib/{generators → rails/generators}/rails/app/app_generator.rb +21 -7
- data/lib/rails/generators/rails/app/templates/Gemfile +34 -0
- data/lib/{generators → rails/generators}/rails/app/templates/README +9 -8
- data/lib/{generators → rails/generators}/rails/app/templates/Rakefile +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/app/controllers/application_controller.rb +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/app/helpers/application_helper.rb +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/app/models/.empty_directory +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/app/views/layouts/.empty_directory +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config.ru +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/application.rb +4 -3
- data/lib/rails/generators/rails/app/templates/config/boot.rb +14 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/databases/frontbase.yml +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/databases/ibm_db.yml +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/databases/mysql.yml +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/databases/oracle.yml +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/databases/postgresql.yml +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/databases/sqlite3.yml +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/environment.rb +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/environments/development.rb.tt +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/environments/production.rb.tt +9 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/environments/test.rb.tt +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/initializers/backtrace_silencers.rb +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/initializers/cookie_verification_secret.rb.tt +1 -1
- data/lib/{generators → rails/generators}/rails/app/templates/config/initializers/inflections.rb +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/initializers/mime_types.rb +0 -0
- data/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt +10 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/locales/en.yml +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/routes.rb +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/db/seeds.rb +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/doc/README_FOR_APP +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/gitignore +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/public/404.html +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/public/422.html +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/public/500.html +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/public/favicon.ico +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/public/images/rails.png +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/public/index.html +17 -17
- data/lib/{generators → rails/generators}/rails/app/templates/public/javascripts/application.js +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/public/javascripts/controls.js +5 -3
- data/lib/{generators → rails/generators}/rails/app/templates/public/javascripts/dragdrop.js +7 -6
- data/lib/{generators → rails/generators}/rails/app/templates/public/javascripts/effects.js +8 -13
- data/lib/{generators → rails/generators}/rails/app/templates/public/javascripts/prototype.js +1573 -1019
- data/lib/{generators → rails/generators}/rails/app/templates/public/javascripts/rails.js +1 -2
- data/lib/{generators → rails/generators}/rails/app/templates/public/robots.txt +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/public/stylesheets/.empty_directory +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/script/rails +0 -1
- data/lib/{generators → rails/generators}/rails/app/templates/test/fixtures/.empty_directory +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/test/functional/.empty_directory +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/test/integration/.empty_directory +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/test/performance/browsing_test.rb +1 -1
- data/lib/{generators → rails/generators}/rails/app/templates/test/test_helper.rb +1 -1
- data/lib/{generators → rails/generators}/rails/app/templates/test/unit/.empty_directory +0 -0
- data/lib/{generators → rails/generators}/rails/controller/USAGE +1 -1
- data/lib/{generators → rails/generators}/rails/controller/controller_generator.rb +6 -0
- data/lib/{generators → rails/generators}/rails/controller/templates/controller.rb +0 -0
- data/lib/{generators → rails/generators}/rails/generator/USAGE +1 -1
- data/lib/{generators → rails/generators}/rails/generator/generator_generator.rb +0 -0
- data/lib/{generators → rails/generators}/rails/generator/templates/%file_name%_generator.rb.tt +0 -0
- data/lib/{generators → rails/generators}/rails/generator/templates/USAGE.tt +1 -1
- data/lib/{generators → rails/generators}/rails/generator/templates/templates/.empty_directory +0 -0
- data/lib/{generators → rails/generators}/rails/helper/USAGE +1 -1
- data/lib/{generators → rails/generators}/rails/helper/helper_generator.rb +0 -0
- data/lib/{generators → rails/generators}/rails/helper/templates/helper.rb +0 -0
- data/lib/{generators → rails/generators}/rails/integration_test/USAGE +1 -1
- data/lib/{generators → rails/generators}/rails/integration_test/integration_test_generator.rb +0 -0
- data/lib/{generators → rails/generators}/rails/mailer/USAGE +1 -1
- data/lib/{generators → rails/generators}/rails/mailer/mailer_generator.rb +0 -0
- data/lib/{generators → rails/generators}/rails/mailer/templates/mailer.rb +0 -0
- data/lib/{generators → rails/generators}/rails/metal/USAGE +1 -1
- data/lib/{generators → rails/generators}/rails/metal/metal_generator.rb +0 -0
- data/lib/{generators → rails/generators}/rails/metal/templates/metal.rb +0 -0
- data/lib/{generators → rails/generators}/rails/migration/USAGE +2 -2
- data/lib/{generators → rails/generators}/rails/migration/migration_generator.rb +0 -0
- data/lib/{generators → rails/generators}/rails/model/USAGE +2 -2
- data/lib/{generators → rails/generators}/rails/model/model_generator.rb +0 -0
- data/lib/{generators → rails/generators}/rails/observer/USAGE +1 -1
- data/lib/{generators → rails/generators}/rails/observer/observer_generator.rb +0 -0
- data/lib/{generators → rails/generators}/rails/performance_test/USAGE +1 -1
- data/lib/{generators → rails/generators}/rails/performance_test/performance_test_generator.rb +0 -0
- data/lib/{generators → rails/generators}/rails/plugin/USAGE +1 -1
- data/lib/{generators → rails/generators}/rails/plugin/plugin_generator.rb +1 -1
- data/lib/{generators → rails/generators}/rails/plugin/templates/MIT-LICENSE.tt +0 -0
- data/lib/{generators → rails/generators}/rails/plugin/templates/README.tt +0 -0
- data/lib/rails/generators/rails/plugin/templates/Rakefile.tt +23 -0
- data/lib/{generators → rails/generators}/rails/plugin/templates/init.rb +0 -0
- data/lib/{generators → rails/generators}/rails/plugin/templates/install.rb +0 -0
- data/lib/{generators → rails/generators}/rails/plugin/templates/lib/%file_name%.rb.tt +0 -0
- data/lib/{generators → rails/generators}/rails/plugin/templates/lib/tasks/%file_name%_tasks.rake.tt +0 -0
- data/lib/{generators → rails/generators}/rails/plugin/templates/uninstall.rb +0 -0
- data/lib/{generators → rails/generators}/rails/resource/USAGE +3 -3
- data/lib/{generators → rails/generators}/rails/resource/resource_generator.rb +3 -1
- data/lib/{generators → rails/generators}/rails/scaffold/USAGE +4 -4
- data/lib/{generators → rails/generators}/rails/scaffold/scaffold_generator.rb +1 -1
- data/lib/{generators → rails/generators}/rails/scaffold_controller/USAGE +1 -1
- data/lib/{generators → rails/generators}/rails/scaffold_controller/scaffold_controller_generator.rb +0 -0
- data/lib/{generators → rails/generators}/rails/scaffold_controller/templates/controller.rb +2 -2
- data/lib/{generators → rails/generators}/rails/session_migration/USAGE +1 -1
- data/lib/{generators → rails/generators}/rails/session_migration/session_migration_generator.rb +0 -0
- data/lib/{generators → rails/generators}/rails/stylesheets/USAGE +1 -1
- data/lib/{generators → rails/generators}/rails/stylesheets/stylesheets_generator.rb +0 -0
- data/lib/{generators → rails/generators}/rails/stylesheets/templates/scaffold.css +4 -0
- data/lib/{generators → rails/generators}/test_unit.rb +0 -0
- data/lib/{generators → rails/generators}/test_unit/controller/controller_generator.rb +2 -1
- data/lib/rails/generators/test_unit/controller/templates/functional_test.rb +18 -0
- data/lib/{generators → rails/generators}/test_unit/helper/helper_generator.rb +1 -1
- data/lib/{generators → rails/generators}/test_unit/helper/templates/helper_test.rb +0 -0
- data/lib/{generators → rails/generators}/test_unit/integration/integration_generator.rb +1 -1
- data/lib/{generators → rails/generators}/test_unit/integration/templates/integration_test.rb +0 -0
- data/lib/{generators → rails/generators}/test_unit/mailer/mailer_generator.rb +1 -8
- data/lib/rails/generators/test_unit/mailer/templates/functional_test.rb +20 -0
- data/lib/{generators → rails/generators}/test_unit/model/model_generator.rb +1 -1
- data/lib/{generators → rails/generators}/test_unit/model/templates/fixtures.yml +0 -0
- data/lib/{generators → rails/generators}/test_unit/model/templates/unit_test.rb +0 -0
- data/lib/{generators → rails/generators}/test_unit/observer/observer_generator.rb +1 -1
- data/lib/{generators → rails/generators}/test_unit/observer/templates/unit_test.rb +0 -0
- data/lib/{generators → rails/generators}/test_unit/performance/performance_generator.rb +1 -1
- data/lib/{generators → rails/generators}/test_unit/performance/templates/performance_test.rb +0 -0
- data/lib/{generators → rails/generators}/test_unit/plugin/plugin_generator.rb +1 -1
- data/lib/{generators → rails/generators}/test_unit/plugin/templates/%file_name%_test.rb.tt +0 -0
- data/lib/{generators → rails/generators}/test_unit/plugin/templates/test_helper.rb +0 -0
- data/lib/{generators → rails/generators}/test_unit/scaffold/scaffold_generator.rb +1 -1
- data/lib/{generators → rails/generators}/test_unit/scaffold/templates/functional_test.rb +9 -5
- data/{builtin/rails_info → lib}/rails/info.rb +0 -0
- data/{builtin/rails_info → lib}/rails/info_controller.rb +0 -0
- data/{builtin/routes.rb → lib/rails/info_routes.rb} +2 -2
- data/lib/rails/{subscriber.rb → log_subscriber.rb} +27 -27
- data/lib/rails/{subscriber → log_subscriber}/test_helper.rb +15 -16
- data/lib/rails/plugin.rb +31 -8
- data/lib/rails/rack/debugger.rb +3 -1
- data/lib/rails/rack/logger.rb +4 -4
- data/lib/rails/railtie.rb +179 -16
- data/lib/rails/railtie/configuration.rb +56 -1
- data/lib/rails/tasks/documentation.rake +38 -20
- data/lib/rails/tasks/framework.rake +16 -9
- data/lib/rails/tasks/misc.rake +3 -5
- data/lib/rails/tasks/routes.rake +2 -2
- data/lib/rails/test_help.rb +21 -1
- data/lib/rails/test_unit/railtie.rb +1 -3
- data/lib/rails/version.rb +3 -2
- metadata +199 -171
- data/builtin/rails_info/rails/info_helper.rb +0 -2
- data/lib/generators/erb.rb +0 -8
- data/lib/generators/erb/mailer/mailer_generator.rb +0 -20
- data/lib/generators/rails/app/templates/Gemfile +0 -34
- data/lib/generators/rails/app/templates/config/boot.rb +0 -17
- data/lib/generators/rails/app/templates/config/initializers/session_store.rb.tt +0 -15
- data/lib/generators/rails/model_subclass/model_subclass_generator.rb +0 -12
- data/lib/generators/rails/plugin/templates/Rakefile.tt +0 -10
- data/lib/generators/test_unit/controller/templates/functional_test.rb +0 -8
- data/lib/generators/test_unit/mailer/templates/fixture +0 -3
- data/lib/generators/test_unit/mailer/templates/functional_test.rb +0 -22
- data/lib/rails/railties_path.rb +0 -1
@@ -4,7 +4,7 @@ h2. Ruby on Rails Guides
|
|
4
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
5
|
|
6
6
|
* "Current Release version":http://guides.rubyonrails.org - based on Rails 2.3
|
7
|
-
* "Edge version":http://
|
7
|
+
* "Edge version":http://edgeguides.rubyonrails.org - based on the current Rails "master branch":http://github.com/rails/rails/tree/master
|
8
8
|
|
9
9
|
<% end %>
|
10
10
|
|
@@ -120,5 +120,20 @@ h3. Digging Deeper
|
|
120
120
|
<% guide("Contributing to Rails", 'contributing_to_rails.html') do %>
|
121
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
122
|
<% end %>
|
123
|
+
</dl>
|
124
|
+
|
125
|
+
h3. Release Notes
|
126
|
+
|
127
|
+
<dl>
|
128
|
+
<% guide("Ruby on Rails 3.0 Release Notes", '3_0_release_notes.html') do %>
|
129
|
+
Release notes for Rails 3.0.
|
130
|
+
<% end %>
|
131
|
+
|
132
|
+
<% guide("Ruby on Rails 2.3 Release Notes", '2_3_release_notes.html') do %>
|
133
|
+
Release notes for Rails 2.3.
|
134
|
+
<% end %>
|
123
135
|
|
136
|
+
<% guide("Ruby on Rails 2.2 Release Notes", '2_2_release_notes.html') do %>
|
137
|
+
Release notes for Rails 2.2.
|
138
|
+
<% end %>
|
124
139
|
</dl>
|
@@ -67,6 +67,12 @@
|
|
67
67
|
<dd><a href="caching_with_rails.html">Caching with Rails</a></dd>
|
68
68
|
<dd><a href="contributing_to_rails.html">Contributing to Rails</a></dd>
|
69
69
|
</dl>
|
70
|
+
<dl class="R">
|
71
|
+
<dt>Release Notes</dt>
|
72
|
+
<dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
|
73
|
+
<dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
|
74
|
+
<dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
|
75
|
+
</dl>
|
70
76
|
</div>
|
71
77
|
</li>
|
72
78
|
<li><a href="contribute.html">Contribute</a></li>
|
@@ -87,7 +93,7 @@
|
|
87
93
|
<div id="container">
|
88
94
|
<div class="wrapper">
|
89
95
|
<div id="mainCol">
|
90
|
-
<%= yield.html_safe
|
96
|
+
<%= yield.html_safe %>
|
91
97
|
</div>
|
92
98
|
</div>
|
93
99
|
</div>
|
@@ -27,21 +27,74 @@ I'll cover each of these methods in turn. But first, a few words about the very
|
|
27
27
|
|
28
28
|
h4. Rendering by Default: Convention Over Configuration in Action
|
29
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
|
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 valid routes. For example, if you have this code in your +BooksController+ class:
|
31
31
|
|
32
32
|
<ruby>
|
33
|
-
|
34
|
-
|
33
|
+
class BooksController < ApplicationController
|
34
|
+
end
|
35
|
+
</ruby>
|
36
|
+
|
37
|
+
And the following in your routes file:
|
38
|
+
|
39
|
+
<ruby>
|
40
|
+
resources :books
|
41
|
+
</ruby>
|
42
|
+
|
43
|
+
And you have a view file +app/views/books/index.html.erb+:
|
44
|
+
|
45
|
+
<ruby>
|
46
|
+
<h1>Books are coming soon!</h1>
|
47
|
+
</ruby>
|
48
|
+
|
49
|
+
Rails will automatically render +app/views/books/index.html.erb+ when you navigate to +/books+ and you will see on your screen that "Books are coming soon!"
|
50
|
+
|
51
|
+
However a coming soon screen is only minimally useful, so you will soon create your +Book+ model and add the index action to +BooksController+:
|
52
|
+
|
53
|
+
<ruby>
|
54
|
+
class BooksController < ApplicationController
|
55
|
+
def index
|
56
|
+
@books = Book.all
|
57
|
+
end
|
35
58
|
end
|
36
59
|
</ruby>
|
37
60
|
|
38
|
-
|
61
|
+
Note that again, we have convention over configuration, in that there is no explicit render at the end of this index action. The rule is that if you do not explicitly render something by the end of the controller action, rails will look for the +action_name.html.erb+ template in the controllers view path and then render that, so in this case, Rails will render the +app/views/books/index.html.erb+ file.
|
39
62
|
|
40
|
-
|
63
|
+
So in our view, we want to display the properties of all the books, we could do this with an ERB template like this:
|
64
|
+
|
65
|
+
<ruby>
|
66
|
+
<h1>Listing Books</h1>
|
67
|
+
|
68
|
+
<table>
|
69
|
+
<tr>
|
70
|
+
<th>Title</th>
|
71
|
+
<th>Summary</th>
|
72
|
+
<th></th>
|
73
|
+
<th></th>
|
74
|
+
<th></th>
|
75
|
+
</tr>
|
76
|
+
|
77
|
+
<% @books.each do |book| %>
|
78
|
+
<tr>
|
79
|
+
<td><%= book.title %></td>
|
80
|
+
<td><%= book.content %></td>
|
81
|
+
<td><%= link_to 'Show', book %></td>
|
82
|
+
<td><%= link_to 'Edit', edit_book_path(book) %></td>
|
83
|
+
<td><%= link_to 'Remove', book, :confirm => 'Are you sure?', :method => :delete %></td>
|
84
|
+
</tr>
|
85
|
+
<% end %>
|
86
|
+
</table>
|
87
|
+
|
88
|
+
<br />
|
89
|
+
|
90
|
+
<%= link_to 'New book', new_book_path %>
|
91
|
+
</ruby>
|
92
|
+
|
93
|
+
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).
|
41
94
|
|
42
95
|
h4. Using +render+
|
43
96
|
|
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
|
97
|
+
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 customise the behaviour 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
98
|
|
46
99
|
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
100
|
|
@@ -53,7 +106,24 @@ Perhaps the simplest thing you can do with +render+ is to render nothing at all:
|
|
53
106
|
render :nothing => true
|
54
107
|
</ruby>
|
55
108
|
|
56
|
-
|
109
|
+
If you look at the response for this using Curl you will see the following:
|
110
|
+
|
111
|
+
<shell>
|
112
|
+
$ curl -i 127.0.0.1:3000/books
|
113
|
+
HTTP/1.1 200 OK
|
114
|
+
Connection: close
|
115
|
+
Date: Sun, 24 Jan 2010 09:25:18 GMT
|
116
|
+
Transfer-Encoding: chunked
|
117
|
+
Content-Type: */*; charset=utf-8
|
118
|
+
X-Runtime: 0.014297
|
119
|
+
Set-Cookie: _blog_session=...snip...; path=/; HttpOnly
|
120
|
+
Cache-Control: no-cache
|
121
|
+
|
122
|
+
|
123
|
+
$
|
124
|
+
</shell>
|
125
|
+
|
126
|
+
We see there is an empty response (no data after the +Cache-Control+ line), but that Rails has set the response to 200 OK, so the request was successful. You can set the +:status+ options on render to change this response. Rendering nothing can be useful for AJAX requests where all you want to send back to the browser is an acknowledgement that the request was completed.
|
57
127
|
|
58
128
|
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
129
|
|
@@ -73,7 +143,7 @@ def update
|
|
73
143
|
end
|
74
144
|
</ruby>
|
75
145
|
|
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.
|
146
|
+
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
147
|
|
78
148
|
If you prefer, you can use a symbol instead of a string to specify the action to render:
|
79
149
|
|
@@ -89,7 +159,7 @@ def update
|
|
89
159
|
end
|
90
160
|
</ruby>
|
91
161
|
|
92
|
-
To be explicit, you can use +render+ with the +:action+ option (though this is no longer necessary
|
162
|
+
To be explicit, you can use +render+ with the +:action+ option (though this is no longer necessary in Rails 3.0):
|
93
163
|
|
94
164
|
<ruby>
|
95
165
|
def update
|
@@ -140,6 +210,31 @@ NOTE: By default, the file is rendered without using the current layout. If you
|
|
140
210
|
|
141
211
|
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
212
|
|
213
|
+
h5. Wrapping it up
|
214
|
+
|
215
|
+
The above three methods of render (rendering another template within the controller, rendering a template within another controller and rendering an arbitrary file on the file system) are actually all variants of the same action.
|
216
|
+
|
217
|
+
In fact, in the BooksController method, inside of the edit action where we want to render the edit template if the book does not update successfully, all of the following render calls would all render the +edit.html.erb+ template in the +views/books+ directory:
|
218
|
+
|
219
|
+
<ruby>
|
220
|
+
render :edit
|
221
|
+
render :action => :edit
|
222
|
+
render 'edit'
|
223
|
+
render 'edit.html.erb'
|
224
|
+
render :action => 'edit'
|
225
|
+
render :action => 'edit.html.erb'
|
226
|
+
render 'books/edit'
|
227
|
+
render 'books/edit.html.erb'
|
228
|
+
render :template => 'books/edit'
|
229
|
+
render :template => 'books/edit.html.erb'
|
230
|
+
render '/path/to/rails/app/views/books/edit'
|
231
|
+
render '/path/to/rails/app/views/books/edit.html.erb'
|
232
|
+
render :file => '/path/to/rails/app/views/books/edit'
|
233
|
+
render :file => '/path/to/rails/app/views/books/edit.html.erb'
|
234
|
+
</ruby>
|
235
|
+
|
236
|
+
Which one you use is really a matter of style and convention, but the rule of thumb is to use the simplest one that makes sense for the code you are writing.
|
237
|
+
|
143
238
|
h5. Using +render+ with +:inline+
|
144
239
|
|
145
240
|
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:
|
@@ -180,7 +275,7 @@ render :text => "OK"
|
|
180
275
|
|
181
276
|
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
277
|
|
183
|
-
NOTE: By default, if you use the +:text+ option
|
278
|
+
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.
|
184
279
|
|
185
280
|
h5. Rendering JSON
|
186
281
|
|
@@ -314,21 +409,21 @@ end
|
|
314
409
|
|
315
410
|
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
411
|
|
412
|
+
You can also decide the layout by passing a Proc object, the block you give the Proc will be given the +controller+ instance, so you can make decisions based on the current request. For example:
|
413
|
+
|
317
414
|
<ruby>
|
318
415
|
class ProductsController < ApplicationController
|
319
|
-
layout
|
320
|
-
# ...
|
416
|
+
layout Proc.new { |controller| controller.request.xhr? ? 'popup' : 'application' }
|
321
417
|
end
|
322
418
|
</ruby>
|
323
419
|
|
324
420
|
h6. Conditional Layouts
|
325
421
|
|
326
|
-
Layouts specified at the controller level support +:only+ and +:except+ options that take either a method name or an array of method names:
|
422
|
+
Layouts specified at the controller level support +:only+ and +:except+ options that take either a method name or an array of method names which correspond to method names within the controller:
|
327
423
|
|
328
424
|
<ruby>
|
329
425
|
class ProductsController < ApplicationController
|
330
426
|
layout "product", :except => [:index, :rss]
|
331
|
-
#...
|
332
427
|
end
|
333
428
|
</ruby>
|
334
429
|
|
@@ -343,7 +438,6 @@ Layouts are shared downwards in the hierarchy, and more specific layouts always
|
|
343
438
|
<ruby>
|
344
439
|
class ApplicationController < ActionController::Base
|
345
440
|
layout "main"
|
346
|
-
#...
|
347
441
|
end
|
348
442
|
</ruby>
|
349
443
|
|
@@ -351,7 +445,6 @@ end
|
|
351
445
|
|
352
446
|
<ruby>
|
353
447
|
class PostsController < ApplicationController
|
354
|
-
# ...
|
355
448
|
end
|
356
449
|
</ruby>
|
357
450
|
|
@@ -360,7 +453,6 @@ end
|
|
360
453
|
<ruby>
|
361
454
|
class SpecialPostsController < PostsController
|
362
455
|
layout "special"
|
363
|
-
# ...
|
364
456
|
end
|
365
457
|
</ruby>
|
366
458
|
|
@@ -418,6 +510,8 @@ def show
|
|
418
510
|
end
|
419
511
|
</ruby>
|
420
512
|
|
513
|
+
Make sure you use +and return+ and not +&& return+ because while the former will work, the latter will not due to operator precedence in the Ruby Language.
|
514
|
+
|
421
515
|
Note that the implicit render done by ActionController detects if +render+ has been called, and thus avoids this error. Therefore, the following will work without errors:
|
422
516
|
|
423
517
|
<ruby>
|
@@ -463,11 +557,11 @@ Consider these actions to see the difference:
|
|
463
557
|
|
464
558
|
<ruby>
|
465
559
|
def index
|
466
|
-
@books = Book.
|
560
|
+
@books = Book.all
|
467
561
|
end
|
468
562
|
|
469
563
|
def show
|
470
|
-
@book = Book.
|
564
|
+
@book = Book.find_by_id(params[:id])
|
471
565
|
if @book.nil?
|
472
566
|
render :action => "index"
|
473
567
|
end
|
@@ -478,19 +572,39 @@ With the code in this form, there will be likely be a problem if the +@book+ var
|
|
478
572
|
|
479
573
|
<ruby>
|
480
574
|
def index
|
481
|
-
@books = Book.
|
575
|
+
@books = Book.all
|
482
576
|
end
|
483
577
|
|
484
578
|
def show
|
485
|
-
@book = Book.
|
579
|
+
@book = Book.find_by_id(params[:id])
|
486
580
|
if @book.nil?
|
487
|
-
redirect_to :action =>
|
581
|
+
redirect_to :action => :index
|
488
582
|
end
|
489
583
|
end
|
490
584
|
</ruby>
|
491
585
|
|
492
586
|
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
587
|
|
588
|
+
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 301 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.
|
589
|
+
|
590
|
+
While in a small app, this added latency might not be a problem, it is something to think about when speed of response is of the essence. One way to handle this double request (though a contrived example) could be:
|
591
|
+
|
592
|
+
<ruby>
|
593
|
+
def index
|
594
|
+
@books = Book.all
|
595
|
+
end
|
596
|
+
|
597
|
+
def show
|
598
|
+
@book = Book.find_by_id(params[:id])
|
599
|
+
if @book.nil?
|
600
|
+
@books = Book.all
|
601
|
+
render "index", :alert => 'Your book was not found!'
|
602
|
+
end
|
603
|
+
end
|
604
|
+
</ruby>
|
605
|
+
|
606
|
+
Which would detect that there are no books populate the +@books+ instance variable with all the books in the database and then directly render the +index.html.erb+ template returning it to the browser with a flash alert message telling the user what happened.
|
607
|
+
|
494
608
|
h4. Using +head+ To Build Header-Only Responses
|
495
609
|
|
496
610
|
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:
|
@@ -499,12 +613,39 @@ The +head+ method exists to let you send back responses to the browser that have
|
|
499
613
|
head :bad_request
|
500
614
|
</ruby>
|
501
615
|
|
616
|
+
Which would produce the following header:
|
617
|
+
|
618
|
+
<shell>
|
619
|
+
HTTP/1.1 400 Bad Request
|
620
|
+
Connection: close
|
621
|
+
Date: Sun, 24 Jan 2010 12:15:53 GMT
|
622
|
+
Transfer-Encoding: chunked
|
623
|
+
Content-Type: text/html; charset=utf-8
|
624
|
+
X-Runtime: 0.013483
|
625
|
+
Set-Cookie: _blog_session=...snip...; path=/; HttpOnly
|
626
|
+
Cache-Control: no-cache
|
627
|
+
</shell>
|
628
|
+
|
502
629
|
Or you can use other HTTP headers to convey additional information:
|
503
630
|
|
504
631
|
<ruby>
|
505
632
|
head :created, :location => photo_path(@photo)
|
506
633
|
</ruby>
|
507
634
|
|
635
|
+
Which would produce:
|
636
|
+
|
637
|
+
<shell>
|
638
|
+
HTTP/1.1 201 Created
|
639
|
+
Connection: close
|
640
|
+
Date: Sun, 24 Jan 2010 12:16:44 GMT
|
641
|
+
Transfer-Encoding: chunked
|
642
|
+
Location: /photos/1
|
643
|
+
Content-Type: text/html; charset=utf-8
|
644
|
+
X-Runtime: 0.083496
|
645
|
+
Set-Cookie: _blog_session=...snip...; path=/; HttpOnly
|
646
|
+
Cache-Control: no-cache
|
647
|
+
</shell>
|
648
|
+
|
508
649
|
h3. Structuring Layouts
|
509
650
|
|
510
651
|
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:
|
@@ -517,12 +658,14 @@ I'll discuss each of these in turn.
|
|
517
658
|
|
518
659
|
h4. Asset Tags
|
519
660
|
|
520
|
-
Asset tags provide methods for generating HTML that links views to assets like images, javascript, stylesheets, and feeds. There are
|
661
|
+
Asset tags provide methods for generating HTML that links views to assets like images, videos, audio, javascript, stylesheets, and feeds. There are six types of include tag:
|
521
662
|
|
522
663
|
* +auto_discovery_link_tag+
|
523
664
|
* +javascript_include_tag+
|
524
665
|
* +stylesheet_link_tag+
|
525
666
|
* +image_tag+
|
667
|
+
* +video_tag+
|
668
|
+
* +audio_tag+
|
526
669
|
|
527
670
|
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
671
|
|
@@ -663,10 +806,10 @@ You can even use dynamic paths such as +cache/#{current_site}/main/display+.
|
|
663
806
|
|
664
807
|
h5. Linking to Images with +image_tag+
|
665
808
|
|
666
|
-
The +image_tag+ helper builds an HTML +<image
|
809
|
+
The +image_tag+ helper builds an HTML +<image />+ tag to the specified file. By default, files are loaded from +public/images+, note, you must specify the extension, previous versions of Rails would allow you to just call the image name and would append +.png+ if no extension was given, Rails 3.0 does not.
|
667
810
|
|
668
811
|
<erb>
|
669
|
-
<%= image_tag "header" %>
|
812
|
+
<%= image_tag "header.png" %>
|
670
813
|
</erb>
|
671
814
|
|
672
815
|
You can supply a path to the image if you like:
|
@@ -678,14 +821,93 @@ You can supply a path to the image if you like:
|
|
678
821
|
You can supply a hash of additional HTML options:
|
679
822
|
|
680
823
|
<erb>
|
681
|
-
<%= image_tag "icons/delete.gif", :height => 45 %>
|
824
|
+
<%= image_tag "icons/delete.gif", {:height => 45} %>
|
825
|
+
</erb>
|
826
|
+
|
827
|
+
You can also supply an alternate image to show on mouseover:
|
828
|
+
|
829
|
+
<erb>
|
830
|
+
<%= image_tag "home.gif", :onmouseover => "menu/home_highlight.gif" %>
|
831
|
+
</erb>
|
832
|
+
|
833
|
+
Or alternate text if the user has rendering images turned off in their browser, if you do not specify an explicit alt tag, 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:
|
834
|
+
|
835
|
+
<erb>
|
836
|
+
<%= image_tag "home.gif" %>
|
837
|
+
<%= image_tag "home.gif", :alt => "Home" %>
|
838
|
+
</erb>
|
839
|
+
|
840
|
+
You can also specify a special size tag, in the format "{width}x{height}":
|
841
|
+
|
842
|
+
<erb>
|
843
|
+
<%= image_tag "home.gif", :size => "50x20" %>
|
844
|
+
</erb>
|
845
|
+
|
846
|
+
In addition to the above special tags, you can supply a final hash of standard HTML options, such as +:class+ or +:id+ or +:name+:
|
847
|
+
|
848
|
+
<erb>
|
849
|
+
<%= image_tag "home.gif", :alt => "Go Home",
|
850
|
+
:id => "HomeImage",
|
851
|
+
:class => 'nav_bar' %>
|
852
|
+
</erb>
|
853
|
+
|
854
|
+
h5. Linking to Videos with +video_tag+
|
855
|
+
|
856
|
+
The +video_tag+ helper builds an HTML 5 +<video>+ tag to the specified file. By default, files are loaded from +public/videos+.
|
857
|
+
|
858
|
+
<erb>
|
859
|
+
<%= video_tag "movie.ogg" %>
|
860
|
+
</erb>
|
861
|
+
|
862
|
+
Produces
|
863
|
+
|
864
|
+
<erb>
|
865
|
+
<video src="/videos/movie.ogg" />
|
682
866
|
</erb>
|
683
867
|
|
684
|
-
|
868
|
+
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).
|
685
869
|
|
686
|
-
|
687
|
-
|
688
|
-
* +:
|
870
|
+
The video tag also supports all of the +<video>+ HTML options through the HTML options hash, including:
|
871
|
+
|
872
|
+
* +:poster => 'image_name.png'+, provides an image to put in place of the video before it starts playing.
|
873
|
+
* +:autoplay => true+, starts playing the video on page load.
|
874
|
+
* +:loop => true+, loops the video once it gets to the end.
|
875
|
+
* +:controls => true+, provides browser supplied controls for the user to interact with the video.
|
876
|
+
* +:autobuffer => true+, the video will pre load the file for the user on page load.
|
877
|
+
|
878
|
+
You can also specify multiple videos to play by passing an array of videos to the +video_tag+:
|
879
|
+
|
880
|
+
<erb>
|
881
|
+
<%= video_tag ["trailer.ogg", "movie.ogg"] %>
|
882
|
+
</erb>
|
883
|
+
|
884
|
+
This will produce:
|
885
|
+
|
886
|
+
<erb>
|
887
|
+
<video><source src="trailer.ogg" /><source src="movie.ogg" /></video>
|
888
|
+
</erb>
|
889
|
+
|
890
|
+
h5. Linking to Audio files with +audio_tag+
|
891
|
+
|
892
|
+
The +audio_tag+ helper builds an HTML 5 +<audio>+ tag to the specified file. By default, files are loaded from +public/audios+.
|
893
|
+
|
894
|
+
<erb>
|
895
|
+
<%= audio_tag "music.mp3" %>
|
896
|
+
</erb>
|
897
|
+
|
898
|
+
You can supply a path to the image if you like:
|
899
|
+
|
900
|
+
<erb>
|
901
|
+
<%= image_tag "music/first_song.mp3" %>
|
902
|
+
</erb>
|
903
|
+
|
904
|
+
You can also supply a hash of additional options, such as +:id+, +:class+ etc.
|
905
|
+
|
906
|
+
Like the +video_tag+, the +audio_tag+ has special options:
|
907
|
+
|
908
|
+
* +:autoplay => true+, starts playing the audio on page load
|
909
|
+
* +:controls => true+, provides browser supplied controls for the user to interact with the audio.
|
910
|
+
* +:autobuffer => true+, the audio will pre load the file for the user on page load.
|
689
911
|
|
690
912
|
h4. Understanding +yield+
|
691
913
|
|
@@ -752,13 +974,13 @@ h5. Naming Partials
|
|
752
974
|
To render a partial as part of a view, you use the +render+ method within the view, and include the +:partial+ option:
|
753
975
|
|
754
976
|
<ruby>
|
755
|
-
<%= render
|
977
|
+
<%= render "menu" %>
|
756
978
|
</ruby>
|
757
979
|
|
758
980
|
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
981
|
|
760
982
|
<ruby>
|
761
|
-
<%= render
|
983
|
+
<%= render "shared/menu" %>
|
762
984
|
</ruby>
|
763
985
|
|
764
986
|
That code will pull in the partial from +app/views/shared/_menu.html.erb+.
|
@@ -768,14 +990,14 @@ h5. Using Partials to Simplify Views
|
|
768
990
|
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
991
|
|
770
992
|
<erb>
|
771
|
-
<%= render
|
993
|
+
<%= render "shared/ad_banner" %>
|
772
994
|
|
773
995
|
<h1>Products</h1>
|
774
996
|
|
775
997
|
<p>Here are a few of our fine products:</p>
|
776
998
|
...
|
777
999
|
|
778
|
-
<%= render
|
1000
|
+
<%= render "shared/footer" %>
|
779
1001
|
</erb>
|
780
1002
|
|
781
1003
|
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.
|
@@ -787,7 +1009,7 @@ h5. Partial Layouts
|
|
787
1009
|
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
1010
|
|
789
1011
|
<erb>
|
790
|
-
<%= render
|
1012
|
+
<%= render "link_area", :layout => "graybar" %>
|
791
1013
|
</erb>
|
792
1014
|
|
793
1015
|
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).
|
@@ -801,8 +1023,7 @@ You can also pass local variables into partials, making them even more powerful
|
|
801
1023
|
<erb>
|
802
1024
|
<h1>New zone</h1>
|
803
1025
|
<%= error_messages_for :zone %>
|
804
|
-
<%= render :partial => "form", :locals =>
|
805
|
-
{ :button_label => "Create zone", :zone => @zone } %>
|
1026
|
+
<%= render :partial => "form", :locals => { :zone => @zone } %>
|
806
1027
|
</erb>
|
807
1028
|
|
808
1029
|
* +edit.html.erb+
|
@@ -810,8 +1031,7 @@ You can also pass local variables into partials, making them even more powerful
|
|
810
1031
|
<erb>
|
811
1032
|
<h1>Editing zone</h1>
|
812
1033
|
<%= error_messages_for :zone %>
|
813
|
-
<%= render :partial => "form", :locals =>
|
814
|
-
{ :button_label => "Update zone", :zone => @zone } %>
|
1034
|
+
<%= render :partial => "form", :locals => { :zone => @zone } %>
|
815
1035
|
</erb>
|
816
1036
|
|
817
1037
|
* +_form.html.erb+
|
@@ -823,12 +1043,12 @@ You can also pass local variables into partials, making them even more powerful
|
|
823
1043
|
<%= f.text_field :name %>
|
824
1044
|
</p>
|
825
1045
|
<p>
|
826
|
-
<%= f.submit
|
1046
|
+
<%= f.submit %>
|
827
1047
|
</p>
|
828
1048
|
<% end %>
|
829
1049
|
</erb>
|
830
1050
|
|
831
|
-
Although the same partial will be rendered into both views,
|
1051
|
+
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.
|
832
1052
|
|
833
1053
|
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
1054
|
|
@@ -838,15 +1058,15 @@ Every partial also has a local variable with the same name as the partial (minus
|
|
838
1058
|
|
839
1059
|
Within the +customer+ partial, the +customer+ variable will refer to +@new_customer+ from the parent view.
|
840
1060
|
|
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
|
1061
|
+
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.
|
842
1062
|
|
843
1063
|
If you have an instance of a model to render into a partial, you can use a shorthand syntax:
|
844
1064
|
|
845
1065
|
<erb>
|
846
|
-
<%= render
|
1066
|
+
<%= render @customer %>
|
847
1067
|
</erb>
|
848
1068
|
|
849
|
-
Assuming that the +@customer+ instance variable contains an instance of the +Customer+ model, this will use +_customer.html.erb+ to render it.
|
1069
|
+
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.
|
850
1070
|
|
851
1071
|
h5. Rendering Collections
|
852
1072
|
|
@@ -865,63 +1085,70 @@ Partials are very useful in rendering collections. When you pass a collection to
|
|
865
1085
|
<p>Product Name: <%= product.name %></p>
|
866
1086
|
</erb>
|
867
1087
|
|
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.
|
1088
|
+
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.
|
1089
|
+
|
1090
|
+
In Rails 3.0 there is also a shorthand for this, assuming +@posts+ is a collection of +post+ instances, you can simply do in the +index.html.erb+:
|
869
1091
|
|
870
1092
|
<erb>
|
871
|
-
|
1093
|
+
<h1>Products</h1>
|
1094
|
+
<%= render @products %>
|
872
1095
|
</erb>
|
873
1096
|
|
874
|
-
|
1097
|
+
To produce the same result.
|
875
1098
|
|
876
|
-
|
1099
|
+
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:
|
877
1100
|
|
878
|
-
|
1101
|
+
* +index.html.erb+
|
879
1102
|
|
880
1103
|
<erb>
|
881
|
-
|
882
|
-
|
1104
|
+
<h1>Contacts</h1>
|
1105
|
+
<%= render [customer1, employee1, customer2, employee2] %>
|
883
1106
|
</erb>
|
884
1107
|
|
885
|
-
|
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+
|
1108
|
+
* +customers/_customer.html.erb+
|
890
1109
|
|
891
1110
|
<erb>
|
892
|
-
<
|
893
|
-
<%= render :partial => @products %>
|
1111
|
+
<p>Customer: <%= customer.name %></p>
|
894
1112
|
</erb>
|
895
1113
|
|
896
|
-
* +
|
1114
|
+
* +employees/_employee.html.erb+
|
897
1115
|
|
898
1116
|
<erb>
|
899
|
-
<p>
|
1117
|
+
<p>Employee: <%= employee.name %></p>
|
900
1118
|
</erb>
|
901
1119
|
|
902
|
-
|
1120
|
+
In this case, Rails will use the customer or employee partials as appropriate for each member of the collection.
|
903
1121
|
|
904
|
-
|
1122
|
+
h5. Local Variables
|
1123
|
+
|
1124
|
+
To use a custom local variable name within the partial, specify the +:as+ option in the call to the partial:
|
905
1125
|
|
906
1126
|
<erb>
|
907
|
-
|
908
|
-
<%= render :partial =>
|
909
|
-
[customer1, employee1, customer2, employee2] %>
|
1127
|
+
<%= render :partial => "product", :collection => @products, :as => :item %>
|
910
1128
|
</erb>
|
911
1129
|
|
912
|
-
|
1130
|
+
With this change, you can access an instance of the +@products+ collection as the +item+ local variable within the partial.
|
1131
|
+
|
1132
|
+
You can also pass in arbitrary local variables to any partial you are rendering with the +:locals => {}+ option:
|
913
1133
|
|
914
1134
|
<erb>
|
915
|
-
|
1135
|
+
<%= render :partial => 'products', :collection => @products,
|
1136
|
+
:as => :item, :locals => {:title => "Products Page"} %>
|
916
1137
|
</erb>
|
917
1138
|
|
918
|
-
|
1139
|
+
Would render a partial +_products.html.erb+ once for each instance of +product+ in the +@products+ instance variable passing the instance to the partial as a local variable called +item+ and to each partial, make the local variable +title+ available with the value +Products Page+.
|
1140
|
+
|
1141
|
+
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. This does not work in conjunction with the +:as => :value+ option.
|
1142
|
+
|
1143
|
+
You can also specify a second partial to be rendered between instances of the main partial by using the +:spacer_template+ option:
|
1144
|
+
|
1145
|
+
h5. Spacer Templates
|
919
1146
|
|
920
1147
|
<erb>
|
921
|
-
|
1148
|
+
<%= render @products, :spacer_template => "product_ruler" %>
|
922
1149
|
</erb>
|
923
1150
|
|
924
|
-
|
1151
|
+
Rails will render the +_product_ruler+ partial (with no data passed in to it) between each pair of +_product+ partials.
|
925
1152
|
|
926
1153
|
h4. Using Nested Layouts
|
927
1154
|
|
@@ -964,12 +1191,13 @@ On pages generated by +NewsController+, you want to hide the top menu and add a
|
|
964
1191
|
|
965
1192
|
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
1193
|
|
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
|
1194
|
+
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 you are sure you will not subtemplate the +News+ layout, you can replace the +yield(:news_content) or yield+ with simply +yield+.
|
968
1195
|
|
969
1196
|
h3. Changelog
|
970
1197
|
|
971
1198
|
"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/15
|
972
1199
|
|
1200
|
+
* January 25, 2010: Rails 3.0 Update by "Mikel Lindsaar":credits.html#raasdnil
|
973
1201
|
* December 27, 2008: Merge patch from Rodrigo Rosenfeld Rosas covering subtemplates
|
974
1202
|
* December 27, 2008: Information on new rendering defaults by "Mike Gunderloy":credits.html#mgunderloy
|
975
1203
|
* November 9, 2008: Added partial collection counter by "Mike Gunderloy":credits.html#mgunderloy
|