railties 3.0.3 → 3.0.4.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/guides/assets/stylesheets/main.css +1 -1
- data/guides/rails_guides/helpers.rb +2 -7
- data/guides/source/3_0_release_notes.textile +2 -2
- data/guides/source/action_controller_overview.textile +0 -4
- data/guides/source/action_mailer_basics.textile +9 -11
- data/guides/source/action_view_overview.textile +0 -2
- data/guides/source/active_record_querying.textile +9 -57
- data/guides/source/active_record_validations_callbacks.textile +4 -6
- data/guides/source/active_support_core_extensions.textile +49 -2
- data/guides/source/api_documentation_guidelines.textile +1 -1
- data/guides/source/association_basics.textile +0 -2
- data/guides/source/caching_with_rails.textile +0 -1
- data/guides/source/command_line.textile +2 -3
- data/guides/source/configuring.textile +332 -64
- data/guides/source/contribute.textile +1 -1
- data/guides/source/contributing_to_ruby_on_rails.textile +370 -0
- data/guides/source/debugging_rails_applications.textile +0 -2
- data/guides/source/form_helpers.textile +0 -2
- data/guides/source/generators.textile +262 -9
- data/guides/source/getting_started.textile +0 -2
- data/guides/source/i18n.textile +13 -18
- data/guides/source/index.html.erb +9 -9
- data/guides/source/initialization.textile +4 -4
- data/guides/source/layout.html.erb +27 -2
- data/guides/source/layouts_and_rendering.textile +10 -16
- data/guides/source/migrations.textile +0 -2
- data/guides/source/performance_testing.textile +1 -3
- data/guides/source/plugins.textile +0 -2
- data/guides/source/rails_application_templates.textile +0 -2
- data/guides/source/rails_on_rack.textile +0 -2
- data/guides/source/routing.textile +13 -15
- data/guides/source/security.textile +3 -5
- data/guides/source/testing.textile +16 -12
- data/lib/rails/generators.rb +1 -1
- data/lib/rails/generators/actions.rb +1 -1
- data/lib/rails/generators/base.rb +1 -1
- data/lib/rails/generators/rails/app/app_generator.rb +2 -8
- data/lib/rails/generators/rails/app/templates/Gemfile +1 -1
- data/lib/rails/generators/rails/app/templates/config/boot.rb +3 -10
- data/lib/rails/generators/rails/app/templates/config/databases/sqlite3.yml +1 -1
- data/lib/rails/generators/rails/app/templates/gitignore +1 -1
- data/lib/rails/rack/logger.rb +1 -1
- data/lib/rails/railtie.rb +2 -2
- data/lib/rails/test_help.rb +0 -11
- data/lib/rails/version.rb +4 -3
- metadata +22 -17
- data/guides/source/contributing_to_rails.textile +0 -308
@@ -1473,8 +1473,6 @@ Two very common sources of data that are not UTF-8:
|
|
1473
1473
|
|
1474
1474
|
h3. Changelog
|
1475
1475
|
|
1476
|
-
"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/2
|
1477
|
-
|
1478
1476
|
* August 30, 2010: Minor editing after Rails 3 release by "Joost Baaij":http://www.spacebabies.nl
|
1479
1477
|
* July 12, 2010: Fixes, editing and updating of code samples by "Jaime Iniesta":http://jaimeiniesta.com
|
1480
1478
|
* May 16, 2010: Added a section on configuration gotchas to address common encoding problems that people might have by "Yehuda Katz":http://www.yehudakatz.com
|
data/guides/source/i18n.textile
CHANGED
@@ -127,7 +127,7 @@ If you want to translate your Rails application to a *single language other than
|
|
127
127
|
|
128
128
|
However, you would probably like to *provide support for more locales* in your application. In such case, you need to set and pass the locale between requests.
|
129
129
|
|
130
|
-
WARNING: You may be tempted to store the chosen locale in a _session_ or a
|
130
|
+
WARNING: You may be tempted to store the chosen locale in a _session_ or a <em>cookie</em>. *Do not do so*. The locale should be transparent and a part of the URL. This way you don't break people's basic assumptions about the web itself: if you send a URL of some page to a friend, she should see the same page, same content. A fancy word for this would be that you're being "<em>RESTful</em>":http://en.wikipedia.org/wiki/Representational_State_Transfer. Read more about the RESTful approach in "Stefan Tilkov's articles":http://www.infoq.com/articles/rest-introduction. There may be some exceptions to this rule, which are discussed below.
|
131
131
|
|
132
132
|
The _setting part_ is easy. You can set the locale in a +before_filter+ in the +ApplicationController+ like this:
|
133
133
|
|
@@ -253,7 +253,7 @@ match '/:locale' => 'dashboard#index'
|
|
253
253
|
|
254
254
|
Do take special care about the *order of your routes*, so this route declaration does not "eat" other ones. (You may want to add it directly before the +root :to+ declaration.)
|
255
255
|
|
256
|
-
|
256
|
+
NOTE: Have a look at two plugins which simplify work with routes in this way: Sven Fuchs's "routing_filter":http://github.com/svenfuchs/routing-filter/tree/master and Raul Murciano's "translate_routes":http://github.com/raul/translate_routes/tree/master.
|
257
257
|
|
258
258
|
h4. Setting the Locale from the Client Supplied Information
|
259
259
|
|
@@ -278,7 +278,7 @@ def extract_locale_from_accept_language_header
|
|
278
278
|
end
|
279
279
|
</ruby>
|
280
280
|
|
281
|
-
Of course, in a production environment you would need much more robust code, and could use a plugin such as Iain Hecker's "http_accept_language":http://github.com/iain/http_accept_language/tree/master or even Rack middleware such as Ryan Tomayko's "locale":http://github.com/
|
281
|
+
Of course, in a production environment you would need much more robust code, and could use a plugin such as Iain Hecker's "http_accept_language":http://github.com/iain/http_accept_language/tree/master or even Rack middleware such as Ryan Tomayko's "locale":http://github.com/rack/rack-contrib/blob/master/lib/rack/contrib/locale.rb.
|
282
282
|
|
283
283
|
h5. Using GeoIP (or Similar) Database
|
284
284
|
|
@@ -464,24 +464,24 @@ I18n.t 'message'
|
|
464
464
|
The +translate+ method also takes a +:scope+ option which can contain one or more additional keys that will be used to specify a “namespace” or scope for a translation key:
|
465
465
|
|
466
466
|
<ruby>
|
467
|
-
I18n.t :
|
467
|
+
I18n.t :record_invalid, :scope => [:activerecord, :errors, :messages]
|
468
468
|
</ruby>
|
469
469
|
|
470
|
-
This looks up the +:
|
470
|
+
This looks up the +:record_invalid+ message in the Active Record error messages.
|
471
471
|
|
472
472
|
Additionally, both the key and scopes can be specified as dot-separated keys as in:
|
473
473
|
|
474
474
|
<ruby>
|
475
|
-
I18n.translate
|
475
|
+
I18n.translate "activerecord.errors.messages.record_invalid"
|
476
476
|
</ruby>
|
477
477
|
|
478
478
|
Thus the following calls are equivalent:
|
479
479
|
|
480
480
|
<ruby>
|
481
|
-
I18n.t 'activerecord.errors.messages.
|
482
|
-
I18n.t 'errors.messages.
|
483
|
-
I18n.t :
|
484
|
-
I18n.t :
|
481
|
+
I18n.t 'activerecord.errors.messages.record_invalid'
|
482
|
+
I18n.t 'errors.messages.record_invalid', :scope => :active_record
|
483
|
+
I18n.t :record_invalid, :scope => 'activerecord.errors.messages'
|
484
|
+
I18n.t :record_invalid, :scope => [:activerecord, :errors, :messages]
|
485
485
|
</ruby>
|
486
486
|
|
487
487
|
h5. Defaults
|
@@ -697,7 +697,7 @@ activerecord.errors.models.user.attributes.name.blank
|
|
697
697
|
activerecord.errors.models.user.blank
|
698
698
|
activerecord.errors.messages.blank
|
699
699
|
errors.attributes.name.blank
|
700
|
-
errors.
|
700
|
+
errors.messages.blank
|
701
701
|
</ruby>
|
702
702
|
|
703
703
|
When your models are additionally using inheritance then the messages are looked up in the inheritance chain.
|
@@ -719,7 +719,7 @@ activerecord.errors.models.user.attributes.title.blank
|
|
719
719
|
activerecord.errors.models.user.blank
|
720
720
|
activerecord.errors.messages.blank
|
721
721
|
errors.attributes.title.blank
|
722
|
-
errors.
|
722
|
+
errors.messages.blank
|
723
723
|
</ruby>
|
724
724
|
|
725
725
|
This way you can provide special translations for various error messages at different points in your models inheritance chain and in the attributes, models, or default scopes.
|
@@ -830,7 +830,7 @@ In other contexts you might want to change this behaviour, though. E.g. the defa
|
|
830
830
|
|
831
831
|
<ruby>
|
832
832
|
module I18n
|
833
|
-
def just_raise_that_exception(*args)
|
833
|
+
def self.just_raise_that_exception(*args)
|
834
834
|
raise args.first
|
835
835
|
end
|
836
836
|
end
|
@@ -889,8 +889,3 @@ fn1. Or, to quote "Wikipedia":http://en.wikipedia.org/wiki/Internationalization_
|
|
889
889
|
fn2. Other backends might allow or require to use other formats, e.g. a GetText backend might allow to read GetText files.
|
890
890
|
|
891
891
|
fn3. One of these reasons is that we don't want to imply any unnecessary load for applications that do not need any I18n capabilities, so we need to keep the I18n library as simple as possible for English. Another reason is that it is virtually impossible to implement a one-fits-all solution for all problems related to I18n for all existing languages. So a solution that allows us to exchange the entire implementation easily is appropriate anyway. This also makes it much easier to experiment with custom features and extensions.
|
892
|
-
|
893
|
-
|
894
|
-
h3. Changelog
|
895
|
-
|
896
|
-
"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213/tickets/23
|
@@ -31,7 +31,7 @@ Ruby on Rails Guides
|
|
31
31
|
<div id="subCol">
|
32
32
|
<dl>
|
33
33
|
<dd class="warning">Rails Guides are a result of the ongoing <a href="http://hackfest.rubyonrails.org">Guides hackfest</a>, and a work in progress.</dd>
|
34
|
-
<dd class="
|
34
|
+
<dd class="work-in-progress">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 to the author.</dd>
|
35
35
|
</dl>
|
36
36
|
</div>
|
37
37
|
<% end %>
|
@@ -71,7 +71,7 @@ Ruby on Rails Guides
|
|
71
71
|
<p>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.</p>
|
72
72
|
<% end %>
|
73
73
|
|
74
|
-
<%= guide("Action View Form Helpers", 'form_helpers.html', :
|
74
|
+
<%= guide("Action View Form Helpers", 'form_helpers.html', :work_in_progress => true) do %>
|
75
75
|
<p>Guide to using built-in Form helpers.</p>
|
76
76
|
<% end %>
|
77
77
|
</dl>
|
@@ -100,11 +100,11 @@ Ruby on Rails Guides
|
|
100
100
|
<p>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.</p>
|
101
101
|
<% end %>
|
102
102
|
|
103
|
-
<%= guide("Action Mailer Basics", 'action_mailer_basics.html', :
|
103
|
+
<%= guide("Action Mailer Basics", 'action_mailer_basics.html', :work_in_progress => true) do %>
|
104
104
|
<p>This guide describes how to use Action Mailer to send and receive emails.</p>
|
105
105
|
<% end %>
|
106
106
|
|
107
|
-
<%= guide("Testing Rails Applications", 'testing.html', :
|
107
|
+
<%= guide("Testing Rails Applications", 'testing.html', :work_in_progress => true) do %>
|
108
108
|
<p>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.</p>
|
109
109
|
<% end %>
|
110
110
|
|
@@ -124,11 +124,11 @@ Ruby on Rails Guides
|
|
124
124
|
<p>This guide covers the basic configuration settings for a Rails application.</p>
|
125
125
|
<% end %>
|
126
126
|
|
127
|
-
<%= guide("Rails Command Line Tools and Rake tasks", 'command_line.html', :
|
127
|
+
<%= guide("Rails Command Line Tools and Rake tasks", 'command_line.html', :work_in_progress => true) do %>
|
128
128
|
<p>This guide covers the command line tools and rake tasks provided by Rails.</p>
|
129
129
|
<% end %>
|
130
130
|
|
131
|
-
<%= guide("Caching with Rails", 'caching_with_rails.html', :
|
131
|
+
<%= guide("Caching with Rails", 'caching_with_rails.html', :work_in_progress => true) do %>
|
132
132
|
<p>Various caching techniques provided by Rails.</p>
|
133
133
|
<% end %>
|
134
134
|
</dl>
|
@@ -136,7 +136,7 @@ Ruby on Rails Guides
|
|
136
136
|
<h3>Extending Rails</h3>
|
137
137
|
|
138
138
|
<dl>
|
139
|
-
<%= guide("The Basics of Creating Rails Plugins", 'plugins.html', :
|
139
|
+
<%= guide("The Basics of Creating Rails Plugins", 'plugins.html', :work_in_progress => true) do %>
|
140
140
|
<p>This guide covers how to build a plugin to extend the functionality of Rails.</p>
|
141
141
|
<% end %>
|
142
142
|
|
@@ -151,10 +151,10 @@ Ruby on Rails Guides
|
|
151
151
|
<% end %>
|
152
152
|
</dl>
|
153
153
|
|
154
|
-
<h3>Contributing to Rails</h3>
|
154
|
+
<h3>Contributing to Ruby on Rails</h3>
|
155
155
|
|
156
156
|
<dl>
|
157
|
-
<%= guide("Contributing to Rails", '
|
157
|
+
<%= guide("Contributing to Ruby on Rails", 'contributing_to_ruby_on_rails.html') do %>
|
158
158
|
<p>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.</p>
|
159
159
|
<% end %>
|
160
160
|
|
@@ -108,7 +108,7 @@ Now with Rails 3 we have a Gemfile which defines the basics our application need
|
|
108
108
|
# Bundle edge Rails instead:
|
109
109
|
# gem 'rails', :git => 'git://github.com/rails/rails.git'
|
110
110
|
|
111
|
-
gem 'sqlite3
|
111
|
+
gem 'sqlite3'
|
112
112
|
|
113
113
|
# Use unicorn as the web server
|
114
114
|
# gem 'unicorn'
|
@@ -119,7 +119,7 @@ Now with Rails 3 we have a Gemfile which defines the basics our application need
|
|
119
119
|
# Bundle the extra gems:
|
120
120
|
# gem 'bj'
|
121
121
|
# gem 'nokogiri'
|
122
|
-
# gem 'sqlite3
|
122
|
+
# gem 'sqlite3'
|
123
123
|
# gem 'aws-s3', :require => 'aws/s3'
|
124
124
|
|
125
125
|
# Bundle gems for certain environments:
|
@@ -130,7 +130,7 @@ Now with Rails 3 we have a Gemfile which defines the basics our application need
|
|
130
130
|
|
131
131
|
</ruby>
|
132
132
|
|
133
|
-
Here the only two gems we need are +rails+ and +sqlite3
|
133
|
+
Here the only two gems we need are +rails+ and +sqlite3+, so it seems. This is until you run +bundle pack+. This command freezes all the gems required by your application into _vendor/cache_. The gems installed by default are:
|
134
134
|
|
135
135
|
* abstract-1.0.0.gem
|
136
136
|
* actionmailer-3.0.0.gem
|
@@ -155,7 +155,7 @@ Here the only two gems we need are +rails+ and +sqlite3-ruby+, so it seems. This
|
|
155
155
|
* rails-3.0.0.gem
|
156
156
|
* railties-3.0.0.gem
|
157
157
|
* rake-0.8.7.gem
|
158
|
-
* sqlite3-
|
158
|
+
* sqlite3-1.3.3.gem
|
159
159
|
* text-format-1.0.0.gem
|
160
160
|
* text-hyphen-1.0.0.gem
|
161
161
|
* thor-0.13.7.gem
|
@@ -75,8 +75,8 @@
|
|
75
75
|
<dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
|
76
76
|
<dd><a href="generators.html">Creating and Customizing Rails Generators</a></dd>
|
77
77
|
|
78
|
-
<dt>Contributing to Rails</dt>
|
79
|
-
<dd><a href="
|
78
|
+
<dt>Contributing to Ruby on Rails</dt>
|
79
|
+
<dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
|
80
80
|
<dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
|
81
81
|
<dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a></dd>
|
82
82
|
|
@@ -106,6 +106,31 @@
|
|
106
106
|
<div class="wrapper">
|
107
107
|
<div id="mainCol">
|
108
108
|
<%= yield.html_safe %>
|
109
|
+
|
110
|
+
<h3>Feedback</h3>
|
111
|
+
<p>
|
112
|
+
You're encouraged to help in keeping the quality of this guide.
|
113
|
+
</p>
|
114
|
+
<p>
|
115
|
+
If you see any typos or factual errors you are confident to
|
116
|
+
patch please clone <%= link_to 'docrails', 'https://github.com/lifo/docrails' %>
|
117
|
+
and push the change yourself. That branch of Rails has public write access.
|
118
|
+
Commits are still reviewed, but that happens after you've submitted your
|
119
|
+
contribution. <%= link_to 'docrails', 'https://github.com/lifo/docrails' %> is
|
120
|
+
cross-merged with master periodically.
|
121
|
+
</p>
|
122
|
+
<p>
|
123
|
+
You may also find incomplete content, or stuff that is not up to date.
|
124
|
+
Please do add any missing documentation for master. Check the
|
125
|
+
<%= link_to 'Ruby on Rails Guides Guidelines', 'ruby_on_rails_guides_guidelines.html' %>
|
126
|
+
guide for style and conventions.
|
127
|
+
</p>
|
128
|
+
<p>
|
129
|
+
Issues may also be reported <%= link_to 'in Github', 'https://github.com/lifo/docrails/issues' %>.
|
130
|
+
</p>
|
131
|
+
<p>And last but not least, any kind of discussion regarding Ruby on Rails
|
132
|
+
documentation is very welcome in the <%= link_to 'rubyonrails-docs mailing list', 'http://groups.google.com/group/rubyonrails-docs' %>.
|
133
|
+
</p>
|
109
134
|
</div>
|
110
135
|
</div>
|
111
136
|
</div>
|
@@ -106,7 +106,7 @@ Perhaps the simplest thing you can do with +render+ is to render nothing at all:
|
|
106
106
|
render :nothing => true
|
107
107
|
</ruby>
|
108
108
|
|
109
|
-
If you look at the response for this using
|
109
|
+
If you look at the response for this using cURL, you will see the following:
|
110
110
|
|
111
111
|
<shell>
|
112
112
|
$ curl -i 127.0.0.1:3000/books
|
@@ -123,7 +123,7 @@ Cache-Control: no-cache
|
|
123
123
|
$
|
124
124
|
</shell>
|
125
125
|
|
126
|
-
We see there is an empty response (no data after the +Cache-Control+ line), but
|
126
|
+
We see there is an empty response (no data after the +Cache-Control+ line), but the request was successful because Rails has set the response to 200 OK. You can set the +:status+ option 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.
|
127
127
|
|
128
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.
|
129
129
|
|
@@ -349,7 +349,7 @@ render :status => 500
|
|
349
349
|
render :status => :forbidden
|
350
350
|
</ruby>
|
351
351
|
|
352
|
-
Rails understands
|
352
|
+
Rails understands both numeric status codes and symbols for status codes.
|
353
353
|
|
354
354
|
h6. The +:location+ Option
|
355
355
|
|
@@ -607,7 +607,7 @@ Which would detect that there are no books populate the +@books+ instance variab
|
|
607
607
|
|
608
608
|
h4. Using +head+ To Build Header-Only Responses
|
609
609
|
|
610
|
-
The +head+ method
|
610
|
+
The +head+ method can be used to send responses with only headers to the browser. It provides a more obvious alternative to calling +render :nothing+. The +head+ method takes one parameter, which is interpreted as a hash of header names and values. For example, you can return only an error header:
|
611
611
|
|
612
612
|
<ruby>
|
613
613
|
head :bad_request
|
@@ -654,11 +654,9 @@ When Rails renders a view as a response, it does so by combining the view with t
|
|
654
654
|
* +yield+ and +content_for+
|
655
655
|
* Partials
|
656
656
|
|
657
|
-
I'll discuss each of these in turn.
|
658
|
-
|
659
657
|
h4. Asset Tags
|
660
658
|
|
661
|
-
Asset tags provide methods for generating HTML that links views to
|
659
|
+
Asset tags provide methods for generating HTML that links views to feeds, JavaScript, stylesheets, images, videos and audios. These are the six asset tags available in Rails:
|
662
660
|
|
663
661
|
* +auto_discovery_link_tag+
|
664
662
|
* +javascript_include_tag+
|
@@ -683,7 +681,7 @@ The +auto_discovery_link_tag+ helper builds HTML that most browsers and newsread
|
|
683
681
|
There are three tag options available for +auto_discovery_link_tag+:
|
684
682
|
|
685
683
|
* +:rel+ specifies the +rel+ value in the link (defaults to "alternate")
|
686
|
-
* +:type+ specifies an explicit MIME type. Rails will generate an appropriate MIME type automatically
|
684
|
+
* +:type+ specifies an explicit MIME type. Rails will generate an appropriate MIME type automatically.
|
687
685
|
* +:title+ specifies the title of the link
|
688
686
|
|
689
687
|
h5. Linking to Javascript Files with +javascript_include_tag+
|
@@ -830,7 +828,7 @@ You can also supply an alternate image to show on mouseover:
|
|
830
828
|
<%= image_tag "home.gif", :onmouseover => "menu/home_highlight.gif" %>
|
831
829
|
</erb>
|
832
830
|
|
833
|
-
|
831
|
+
You can supply alternate text for the image which will be used if the user has images turned off in their browser. If you do not specify an alt text explicitly, it defaults to the file name of the file, capitalized and with no extension. For example, these two image tags would return the same code:
|
834
832
|
|
835
833
|
<erb>
|
836
834
|
<%= image_tag "home.gif" %>
|
@@ -940,7 +938,7 @@ The main body of the view will always render into the unnamed +yield+. To render
|
|
940
938
|
|
941
939
|
h4. Using +content_for+
|
942
940
|
|
943
|
-
The +content_for+ method allows you to insert content into a +yield+ block in your layout.
|
941
|
+
The +content_for+ method allows you to insert content into a named +yield+ block in your layout. For example, this view would work with the layout that you just saw:
|
944
942
|
|
945
943
|
<erb>
|
946
944
|
<% content_for :head do %>
|
@@ -967,7 +965,7 @@ The +content_for+ method is very helpful when your layout contains distinct regi
|
|
967
965
|
|
968
966
|
h4. Using Partials
|
969
967
|
|
970
|
-
Partial templates - usually just called "partials" - are another device for breaking
|
968
|
+
Partial templates - usually just called "partials" - are another device for breaking the rendering process into more manageable chunks. With a partial, you can move the code for rendering a particular piece of a response to its own file.
|
971
969
|
|
972
970
|
h5. Naming Partials
|
973
971
|
|
@@ -1087,15 +1085,13 @@ Partials are very useful in rendering collections. When you pass a collection to
|
|
1087
1085
|
|
1088
1086
|
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
1087
|
|
1090
|
-
In Rails 3.0 there is also a shorthand for this
|
1088
|
+
In Rails 3.0, there is also a shorthand for this. Assuming +@products+ is a collection of +product+ instances, you can simply write this in the +index.html.erb+ to produce the same result:
|
1091
1089
|
|
1092
1090
|
<erb>
|
1093
1091
|
<h1>Products</h1>
|
1094
1092
|
<%= render @products %>
|
1095
1093
|
</erb>
|
1096
1094
|
|
1097
|
-
To produce the same result.
|
1098
|
-
|
1099
1095
|
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:
|
1100
1096
|
|
1101
1097
|
* +index.html.erb+
|
@@ -1195,8 +1191,6 @@ There are several ways of getting similar results with different sub-templating
|
|
1195
1191
|
|
1196
1192
|
h3. Changelog
|
1197
1193
|
|
1198
|
-
"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/15
|
1199
|
-
|
1200
1194
|
* April 4, 2010: Fixed document to validate XHTML 1.0 Strict. "Jaime Iniesta":http://jaimeiniesta.com
|
1201
1195
|
* January 25, 2010: Rails 3.0 Update by "Mikel Lindsaar":credits.html#raasdnil
|
1202
1196
|
* December 27, 2008: Merge patch from Rodrigo Rosenfeld Rosas covering subtemplates
|
@@ -590,7 +590,5 @@ Although Active Record does not provide any tools for working directly with such
|
|
590
590
|
|
591
591
|
h3. Changelog
|
592
592
|
|
593
|
-
"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/6
|
594
|
-
|
595
593
|
* July 15, 2010: minor typos corrected by "Jaime Iniesta":http://jaimeiniesta.com
|
596
594
|
* September 14, 2008: initial version by "Frederick Cheung":credits.html#fcheung
|
@@ -398,7 +398,7 @@ $ rails benchmarker 'Item.first' 'Item.last'
|
|
398
398
|
|
399
399
|
h4. +profiler+
|
400
400
|
|
401
|
-
+profiler+ is a wrapper around http://ruby-prof.rubyforge.org
|
401
|
+
+profiler+ is a wrapper around the "ruby-prof":http://ruby-prof.rubyforge.org gem.
|
402
402
|
|
403
403
|
Usage:
|
404
404
|
|
@@ -524,7 +524,5 @@ Rails has been lucky to have two startups dedicated to Rails specific performanc
|
|
524
524
|
|
525
525
|
h3. Changelog
|
526
526
|
|
527
|
-
"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/4
|
528
|
-
|
529
527
|
* January 9, 2009: Complete rewrite by "Pratik":credits.html#lifo
|
530
528
|
* September 6, 2008: Initial version by Matthew Bergman
|
@@ -1509,7 +1509,5 @@ The final plugin should have a directory structure that looks something like thi
|
|
1509
1509
|
|
1510
1510
|
h3. Changelog
|
1511
1511
|
|
1512
|
-
"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213/tickets/32-update-plugins-guide
|
1513
|
-
|
1514
1512
|
* April 4, 2010: Fixed document to validate XHTML 1.0 Strict. "Jaime Iniesta":http://jaimeiniesta.com
|
1515
1513
|
* November 17, 2008: Major revision by Jeff Dean
|
@@ -237,7 +237,5 @@ h4. Understanding Middlewares
|
|
237
237
|
|
238
238
|
h3. Changelog
|
239
239
|
|
240
|
-
"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/58
|
241
|
-
|
242
240
|
* February 7, 2009: Second version by "Pratik":credits.html#lifo
|
243
241
|
* January 11, 2009: First version by "Pratik":credits.html#lifo
|
@@ -3,7 +3,7 @@ h2. Rails Routing from the Outside In
|
|
3
3
|
This guide covers the user-facing features of Rails routing. By referring to this guide, you will be able to:
|
4
4
|
|
5
5
|
* Understand the code in +routes.rb+
|
6
|
-
* Construct your own routes, using either the preferred resourceful style or
|
6
|
+
* Construct your own routes, using either the preferred resourceful style or the <tt>match</tt> method
|
7
7
|
* Identify what parameters to expect an action to receive
|
8
8
|
* Automatically create paths and URLs using route helpers
|
9
9
|
* Use advanced techniques such as constraints and Rack endpoints
|
@@ -50,7 +50,7 @@ Resource routing allows you to quickly declare all of the common routes for a gi
|
|
50
50
|
|
51
51
|
h4. Resources on the Web
|
52
52
|
|
53
|
-
Browsers request pages from Rails by making a request for a URL using a specific HTTP method, such as +GET+, +POST+, +PUT+ and +DELETE+. Each method is a request to perform an operation on the resource. A resource route maps a number of related
|
53
|
+
Browsers request pages from Rails by making a request for a URL using a specific HTTP method, such as +GET+, +POST+, +PUT+ and +DELETE+. Each method is a request to perform an operation on the resource. A resource route maps a number of related requests to actions in a single controller.
|
54
54
|
|
55
55
|
When your Rails application receives an incoming request for
|
56
56
|
|
@@ -194,7 +194,7 @@ end
|
|
194
194
|
or, for a single case
|
195
195
|
|
196
196
|
<ruby>
|
197
|
-
resources :posts, :path => "/admin"
|
197
|
+
resources :posts, :path => "/admin/posts"
|
198
198
|
</ruby>
|
199
199
|
|
200
200
|
In each of these cases, the named routes remain the same as if you did not use +scope+. In the last case, the following paths map to +PostsController+:
|
@@ -470,7 +470,7 @@ This route would match paths such as +/photos/A12345+. You can more succinctly e
|
|
470
470
|
match 'photos/:id' => 'photos#show', :id => /[A-Z]\d{5}/
|
471
471
|
</ruby>
|
472
472
|
|
473
|
-
+:constraints+ takes regular
|
473
|
+
+:constraints+ takes regular expressions with the restriction that regexp anchors can't be used. For example, the following route will not work:
|
474
474
|
|
475
475
|
<ruby>
|
476
476
|
match '/:id' => 'posts#show', :constraints => {:id => /^\d/}
|
@@ -536,7 +536,7 @@ match 'photos/*other' => 'photos#unknown'
|
|
536
536
|
|
537
537
|
This route would match +photos/12+ or +/photos/long/path/to/12+, setting +params[:other]+ to +"12"+ or +"long/path/to/12"+.
|
538
538
|
|
539
|
-
Wildcard segments
|
539
|
+
Wildcard segments can occur anywhere in a route. For example,
|
540
540
|
|
541
541
|
<ruby>
|
542
542
|
match 'books/*section/:title' => 'books#show'
|
@@ -544,7 +544,7 @@ match 'books/*section/:title' => 'books#show'
|
|
544
544
|
|
545
545
|
would match +books/some/section/last-words-a-memoir+ with +params[:section]+ equals +"some/section"+, and +params[:title]+ equals +"last-words-a-memoir"+.
|
546
546
|
|
547
|
-
|
547
|
+
Technically a route can have even more than one wildcard segment. The matcher assigns segments to parameters in an intuitive way. For example,
|
548
548
|
|
549
549
|
<ruby>
|
550
550
|
match '*a/foo/*b' => 'test#index'
|
@@ -595,7 +595,7 @@ You can specify what Rails should route +"/"+ to with the +root+ method:
|
|
595
595
|
root :to => 'pages#main'
|
596
596
|
</ruby>
|
597
597
|
|
598
|
-
You should put the +root+ route at the end of the file.
|
598
|
+
You should put the +root+ route at the end of the file. You also need to delete the public/index.html.erb file for the root route to take effect.
|
599
599
|
|
600
600
|
h3. Customizing Resourceful Routes
|
601
601
|
|
@@ -641,7 +641,7 @@ constraints(:id => /[A-Z][A-Z][0-9]+/) do
|
|
641
641
|
end
|
642
642
|
</ruby>
|
643
643
|
|
644
|
-
NOTE: Of course, you can use the more advanced constraints available in non-resourceful routes in this context
|
644
|
+
NOTE: Of course, you can use the more advanced constraints available in non-resourceful routes in this context.
|
645
645
|
|
646
646
|
h4. Overriding the Named Helpers
|
647
647
|
|
@@ -651,7 +651,7 @@ The +:as+ option lets you override the normal naming for the named route helpers
|
|
651
651
|
resources :photos, :as => "images"
|
652
652
|
</ruby>
|
653
653
|
|
654
|
-
will recognize incoming paths beginning with +/photos+ and route the requests to +PhotosController
|
654
|
+
will recognize incoming paths beginning with +/photos+ and route the requests to +PhotosController+, but use the value of the :as option to name the helpers.
|
655
655
|
|
656
656
|
|_.HTTP verb|_.Path |_.action |_.named helper |
|
657
657
|
|GET |/photos |index | images_path |
|
@@ -679,7 +679,7 @@ This would cause the routing to recognize paths such as
|
|
679
679
|
|
680
680
|
NOTE: The actual action names aren't changed by this option. The two paths shown would still route to the +new+ and +edit+ actions.
|
681
681
|
|
682
|
-
TIP: If you find yourself wanting to change this option uniformly for all of your routes, you can use a scope
|
682
|
+
TIP: If you find yourself wanting to change this option uniformly for all of your routes, you can use a scope.
|
683
683
|
|
684
684
|
<ruby>
|
685
685
|
scope :path_names => { :new => "make" } do
|
@@ -715,7 +715,7 @@ NOTE: The +namespace+ scope will automatically add +:as+ as well as +:module+ an
|
|
715
715
|
|
716
716
|
h4. Restricting the Routes Created
|
717
717
|
|
718
|
-
By default, Rails creates routes for
|
718
|
+
By default, Rails creates routes for the seven default actions (index, show, new, create, edit, update, and destroy) for every RESTful route in your application. You can use the +:only+ and +:except+ options to fine-tune this behavior. The +:only+ option tells Rails to create only the specified routes:
|
719
719
|
|
720
720
|
<ruby>
|
721
721
|
resources :photos, :only => [:index, :show]
|
@@ -816,7 +816,7 @@ Routes should be included in your testing strategy (just like the rest of your a
|
|
816
816
|
|
817
817
|
h5. The +assert_generates+ Assertion
|
818
818
|
|
819
|
-
|
819
|
+
+assert_generates+ asserts that a particular set of options generate a particular path and can be used with default routes or custom routes.
|
820
820
|
|
821
821
|
<ruby>
|
822
822
|
assert_generates "/photos/1", { :controller => "photos", :action => "show", :id => "1" }
|
@@ -825,7 +825,7 @@ assert_generates "/about", :controller => "pages", :action => "about"
|
|
825
825
|
|
826
826
|
h5. The +assert_recognizes+ Assertion
|
827
827
|
|
828
|
-
|
828
|
+
+assert_recognizes+ is the inverse of +assert_generates+. It asserts that a given path is recognized and routes it to a particular spot in your application.
|
829
829
|
|
830
830
|
<ruby>
|
831
831
|
assert_recognizes({ :controller => "photos", :action => "show", :id => "1" }, "/photos/1")
|
@@ -853,8 +853,6 @@ assert_routing({ :path => "photos", :method => :post }, { :controller => "photos
|
|
853
853
|
|
854
854
|
h3. Changelog
|
855
855
|
|
856
|
-
"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/3
|
857
|
-
|
858
856
|
* April 10, 2010: Updated guide to remove outdated and superfluous information, and to provide information about new features, by "Yehuda Katz":http://www.yehudakatz.com
|
859
857
|
* April 2, 2010: Updated guide to match new Routing DSL in Rails 3, by "Rizwan Reza":http://www.rizwanreza.com/
|
860
858
|
* Febuary 1, 2010: Modifies the routing documentation to match new routing DSL in Rails 3, by Prem Sichanugrist
|