rails 4.1.4 → 4.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +12 -10
- data/guides/CHANGELOG.md +15 -25
- data/guides/Rakefile +5 -3
- data/guides/assets/javascripts/guides.js +6 -0
- data/guides/assets/stylesheets/main.css +4 -1
- data/guides/bug_report_templates/action_controller_gem.rb +2 -2
- data/guides/bug_report_templates/action_controller_master.rb +3 -2
- data/guides/rails_guides/helpers.rb +1 -1
- data/guides/rails_guides/levenshtein.rb +29 -21
- data/guides/rails_guides/markdown/renderer.rb +1 -1
- data/guides/rails_guides/markdown.rb +11 -7
- data/guides/rails_guides.rb +2 -2
- data/guides/source/2_2_release_notes.md +1 -1
- data/guides/source/2_3_release_notes.md +4 -4
- data/guides/source/3_0_release_notes.md +8 -8
- data/guides/source/3_1_release_notes.md +5 -2
- data/guides/source/3_2_release_notes.md +6 -3
- data/guides/source/4_0_release_notes.md +6 -3
- data/guides/source/4_1_release_notes.md +10 -11
- data/guides/source/4_2_release_notes.md +850 -0
- data/guides/source/_license.html.erb +1 -1
- data/guides/source/_welcome.html.erb +2 -8
- data/guides/source/action_controller_overview.md +84 -10
- data/guides/source/action_mailer_basics.md +91 -28
- data/guides/source/action_view_overview.md +140 -130
- data/guides/source/active_job_basics.md +318 -0
- data/guides/source/active_model_basics.md +371 -17
- data/guides/source/active_record_basics.md +19 -18
- data/guides/source/active_record_callbacks.md +12 -9
- data/guides/source/{migrations.md → active_record_migrations.md} +135 -226
- data/guides/source/active_record_postgresql.md +433 -0
- data/guides/source/active_record_querying.md +269 -259
- data/guides/source/active_record_validations.md +21 -12
- data/guides/source/active_support_core_extensions.md +113 -73
- data/guides/source/active_support_instrumentation.md +10 -7
- data/guides/source/api_documentation_guidelines.md +62 -16
- data/guides/source/asset_pipeline.md +264 -67
- data/guides/source/association_basics.md +81 -74
- data/guides/source/caching_with_rails.md +32 -7
- data/guides/source/command_line.md +52 -30
- data/guides/source/configuring.md +132 -29
- data/guides/source/constant_autoloading_and_reloading.md +1297 -0
- data/guides/source/contributing_to_ruby_on_rails.md +192 -112
- data/guides/source/credits.html.erb +2 -2
- data/guides/source/debugging_rails_applications.md +448 -294
- data/guides/source/development_dependencies_install.md +47 -36
- data/guides/source/documents.yaml +19 -7
- data/guides/source/engines.md +210 -189
- data/guides/source/form_helpers.md +79 -56
- data/guides/source/generators.md +24 -11
- data/guides/source/getting_started.md +339 -201
- data/guides/source/i18n.md +111 -68
- data/guides/source/index.html.erb +1 -0
- data/guides/source/initialization.md +109 -62
- data/guides/source/layout.html.erb +1 -4
- data/guides/source/layouts_and_rendering.md +18 -17
- data/guides/source/maintenance_policy.md +26 -4
- data/guides/source/nested_model_forms.md +7 -4
- data/guides/source/plugins.md +27 -27
- data/guides/source/rails_application_templates.md +21 -3
- data/guides/source/rails_on_rack.md +12 -9
- data/guides/source/routing.md +100 -74
- data/guides/source/ruby_on_rails_guides_guidelines.md +11 -12
- data/guides/source/security.md +40 -34
- data/guides/source/testing.md +188 -117
- data/guides/source/upgrading_ruby_on_rails.md +284 -29
- data/guides/source/working_with_javascript_in_rails.md +18 -16
- data/guides/w3c_validator.rb +2 -0
- metadata +40 -94
- data/guides/code/getting_started/Gemfile +0 -40
- data/guides/code/getting_started/Gemfile.lock +0 -125
- data/guides/code/getting_started/README.rdoc +0 -28
- data/guides/code/getting_started/Rakefile +0 -6
- data/guides/code/getting_started/app/assets/javascripts/application.js +0 -15
- data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/javascripts/welcome.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/application.css +0 -13
- data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/welcome.css.scss +0 -3
- data/guides/code/getting_started/app/controllers/application_controller.rb +0 -5
- data/guides/code/getting_started/app/controllers/comments_controller.rb +0 -23
- data/guides/code/getting_started/app/controllers/posts_controller.rb +0 -53
- data/guides/code/getting_started/app/controllers/welcome_controller.rb +0 -4
- data/guides/code/getting_started/app/helpers/application_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/comments_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/posts_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/welcome_helper.rb +0 -2
- data/guides/code/getting_started/app/models/comment.rb +0 -3
- data/guides/code/getting_started/app/models/post.rb +0 -7
- data/guides/code/getting_started/app/views/comments/_comment.html.erb +0 -15
- data/guides/code/getting_started/app/views/comments/_form.html.erb +0 -13
- data/guides/code/getting_started/app/views/layouts/application.html.erb +0 -14
- data/guides/code/getting_started/app/views/posts/_form.html.erb +0 -27
- data/guides/code/getting_started/app/views/posts/edit.html.erb +0 -5
- data/guides/code/getting_started/app/views/posts/index.html.erb +0 -21
- data/guides/code/getting_started/app/views/posts/new.html.erb +0 -5
- data/guides/code/getting_started/app/views/posts/show.html.erb +0 -18
- data/guides/code/getting_started/app/views/welcome/index.html.erb +0 -4
- data/guides/code/getting_started/bin/bundle +0 -4
- data/guides/code/getting_started/bin/rails +0 -4
- data/guides/code/getting_started/bin/rake +0 -4
- data/guides/code/getting_started/config/application.rb +0 -18
- data/guides/code/getting_started/config/boot.rb +0 -4
- data/guides/code/getting_started/config/database.yml +0 -25
- data/guides/code/getting_started/config/environment.rb +0 -5
- data/guides/code/getting_started/config/environments/development.rb +0 -30
- data/guides/code/getting_started/config/environments/production.rb +0 -80
- data/guides/code/getting_started/config/environments/test.rb +0 -36
- data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +0 -7
- data/guides/code/getting_started/config/initializers/filter_parameter_logging.rb +0 -4
- data/guides/code/getting_started/config/initializers/inflections.rb +0 -16
- data/guides/code/getting_started/config/initializers/locale.rb +0 -9
- data/guides/code/getting_started/config/initializers/mime_types.rb +0 -5
- data/guides/code/getting_started/config/initializers/secret_token.rb +0 -12
- data/guides/code/getting_started/config/initializers/session_store.rb +0 -3
- data/guides/code/getting_started/config/initializers/wrap_parameters.rb +0 -14
- data/guides/code/getting_started/config/locales/en.yml +0 -23
- data/guides/code/getting_started/config/routes.rb +0 -7
- data/guides/code/getting_started/config.ru +0 -4
- data/guides/code/getting_started/db/migrate/20130122042648_create_posts.rb +0 -10
- data/guides/code/getting_started/db/migrate/20130122045842_create_comments.rb +0 -11
- data/guides/code/getting_started/db/schema.rb +0 -33
- data/guides/code/getting_started/db/seeds.rb +0 -7
- data/guides/code/getting_started/public/404.html +0 -60
- data/guides/code/getting_started/public/422.html +0 -60
- data/guides/code/getting_started/public/500.html +0 -59
- data/guides/code/getting_started/public/favicon.ico +0 -0
- data/guides/code/getting_started/public/robots.txt +0 -5
- data/guides/code/getting_started/test/controllers/comments_controller_test.rb +0 -7
- data/guides/code/getting_started/test/controllers/posts_controller_test.rb +0 -7
- data/guides/code/getting_started/test/controllers/welcome_controller_test.rb +0 -9
- data/guides/code/getting_started/test/fixtures/comments.yml +0 -11
- data/guides/code/getting_started/test/fixtures/posts.yml +0 -9
- data/guides/code/getting_started/test/helpers/comments_helper_test.rb +0 -4
- data/guides/code/getting_started/test/helpers/posts_helper_test.rb +0 -4
- data/guides/code/getting_started/test/helpers/welcome_helper_test.rb +0 -4
- data/guides/code/getting_started/test/models/comment_test.rb +0 -7
- data/guides/code/getting_started/test/models/post_test.rb +0 -7
- data/guides/code/getting_started/test/test_helper.rb +0 -12
@@ -7,7 +7,6 @@ After reading this guide, you will know:
|
|
7
7
|
* How best to use templates, partials, and layouts.
|
8
8
|
* What helpers are provided by Action View and how to make your own.
|
9
9
|
* How to use localized views.
|
10
|
-
* How to use Action View outside of Rails.
|
11
10
|
|
12
11
|
--------------------------------------------------------------------------------
|
13
12
|
|
@@ -28,34 +27,34 @@ For each controller there is an associated directory in the `app/views` director
|
|
28
27
|
Let's take a look at what Rails does by default when creating a new resource using the scaffold generator:
|
29
28
|
|
30
29
|
```bash
|
31
|
-
$ bin/rails generate scaffold
|
30
|
+
$ bin/rails generate scaffold article
|
32
31
|
[...]
|
33
32
|
invoke scaffold_controller
|
34
|
-
create app/controllers/
|
33
|
+
create app/controllers/articles_controller.rb
|
35
34
|
invoke erb
|
36
|
-
create app/views/
|
37
|
-
create app/views/
|
38
|
-
create app/views/
|
39
|
-
create app/views/
|
40
|
-
create app/views/
|
41
|
-
create app/views/
|
35
|
+
create app/views/articles
|
36
|
+
create app/views/articles/index.html.erb
|
37
|
+
create app/views/articles/edit.html.erb
|
38
|
+
create app/views/articles/show.html.erb
|
39
|
+
create app/views/articles/new.html.erb
|
40
|
+
create app/views/articles/_form.html.erb
|
42
41
|
[...]
|
43
42
|
```
|
44
43
|
|
45
44
|
There is a naming convention for views in Rails. Typically, the views share their name with the associated controller action, as you can see above.
|
46
|
-
For example, the index controller action of the `
|
47
|
-
The complete HTML returned to the client is composed of a combination of this ERB file, a layout template that wraps it, and all the partials that the view may reference.
|
45
|
+
For example, the index controller action of the `articles_controller.rb` will use the `index.html.erb` view file in the `app/views/articles` directory.
|
46
|
+
The complete HTML returned to the client is composed of a combination of this ERB file, a layout template that wraps it, and all the partials that the view may reference. Within this guide you will find more detailed documentation about each of these three components.
|
48
47
|
|
49
48
|
|
50
49
|
Templates, Partials and Layouts
|
51
50
|
-------------------------------
|
52
51
|
|
53
|
-
As mentioned
|
54
|
-
Below is a brief overview of each
|
52
|
+
As mentioned, the final HTML output is a composition of three Rails elements: `Templates`, `Partials` and `Layouts`.
|
53
|
+
Below is a brief overview of each of them.
|
55
54
|
|
56
55
|
### Templates
|
57
56
|
|
58
|
-
Action View templates can be written in several ways. If the template file has a `.erb` extension then it uses a mixture of ERB (
|
57
|
+
Action View templates can be written in several ways. If the template file has a `.erb` extension then it uses a mixture of ERB (Embedded Ruby) and HTML. If the template file has a `.builder` extension then the `Builder::XmlMarkup` library is used.
|
59
58
|
|
60
59
|
Rails supports multiple template systems and uses a file extension to distinguish amongst them. For example, an HTML file using the ERB template system will have `.html.erb` as a file extension.
|
61
60
|
|
@@ -72,7 +71,7 @@ Consider the following loop for names:
|
|
72
71
|
<% end %>
|
73
72
|
```
|
74
73
|
|
75
|
-
The loop is set up
|
74
|
+
The loop is set up using regular embedding tags (`<% %>`) and the name is inserted using the output embedding tags (`<%= %>`). Note that this is not just a usage suggestion: regular output functions such as `print` and `puts` won't be rendered to the view with ERB templates. So this would be wrong:
|
76
75
|
|
77
76
|
```html+erb
|
78
77
|
<%# WRONG %>
|
@@ -231,7 +230,7 @@ The `object` and `as` options can also be used together:
|
|
231
230
|
|
232
231
|
#### Rendering Collections
|
233
232
|
|
234
|
-
It is very common that a template
|
233
|
+
It is very common that a template will need to iterate over a collection and render a sub-template for each of the elements. This pattern has been implemented as a single method that accepts an array and renders a partial for each one of the elements in the array.
|
235
234
|
|
236
235
|
So this example for rendering all the products:
|
237
236
|
|
@@ -247,7 +246,7 @@ can be rewritten in a single line:
|
|
247
246
|
<%= render partial: "product", collection: @products %>
|
248
247
|
```
|
249
248
|
|
250
|
-
When a partial is called
|
249
|
+
When a partial is called with a collection, 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 it you can refer to `product` to get the collection member that is being rendered.
|
251
250
|
|
252
251
|
You can use a shorthand syntax for rendering collections. Assuming `@products` is a collection of `Product` instances, you can simply write the following to produce the same result:
|
253
252
|
|
@@ -255,7 +254,7 @@ You can use a shorthand syntax for rendering collections. Assuming `@products` i
|
|
255
254
|
<%= render @products %>
|
256
255
|
```
|
257
256
|
|
258
|
-
Rails determines the name of the partial to use by looking at the model name in the collection, `Product` in this case. In fact, you can even
|
257
|
+
Rails determines the name of the partial to use by looking at the model name in the collection, `Product` in this case. In fact, you can even render a collection made up of instances of different models using this shorthand, and Rails will choose the proper partial for each member of the collection.
|
259
258
|
|
260
259
|
#### Spacer Templates
|
261
260
|
|
@@ -269,30 +268,30 @@ Rails will render the `_product_ruler` partial (with no data passed to it) betwe
|
|
269
268
|
|
270
269
|
### Layouts
|
271
270
|
|
272
|
-
Layouts can be used to render a common view template around the results of Rails controller actions. Typically,
|
271
|
+
Layouts can be used to render a common view template around the results of Rails controller actions. Typically, a Rails application will have a couple of layouts that pages will be rendered within. For example, a site might have one layout for a logged in user and another for the marketing or sales side of the site. The logged in user layout might include top-level navigation that should be present across many controller actions. The sales layout for a SaaS app might include top-level navigation for things like "Pricing" and "Contact Us" pages. You would expect each layout to have a different look and feel. You can read about layouts in more detail in the [Layouts and Rendering in Rails](layouts_and_rendering.html) guide.
|
273
272
|
|
274
273
|
Partial Layouts
|
275
274
|
---------------
|
276
275
|
|
277
|
-
Partials can have their own layouts applied to them. These layouts are different
|
276
|
+
Partials can have their own layouts applied to them. These layouts are different from those applied to a controller action, but they work in a similar fashion.
|
278
277
|
|
279
|
-
Let's say we're displaying
|
278
|
+
Let's say we're displaying an article on a page which should be wrapped in a `div` for display purposes. Firstly, we'll create a new `Article`:
|
280
279
|
|
281
280
|
```ruby
|
282
|
-
|
281
|
+
Article.create(body: 'Partial Layouts are cool!')
|
283
282
|
```
|
284
283
|
|
285
|
-
In the `show` template, we'll render the `
|
284
|
+
In the `show` template, we'll render the `_article` partial wrapped in the `box` layout:
|
286
285
|
|
287
|
-
**
|
286
|
+
**articles/show.html.erb**
|
288
287
|
|
289
288
|
```erb
|
290
|
-
<%= render partial: '
|
289
|
+
<%= render partial: 'article', layout: 'box', locals: {article: @article} %>
|
291
290
|
```
|
292
291
|
|
293
|
-
The `box` layout simply wraps the `
|
292
|
+
The `box` layout simply wraps the `_article` partial in a `div`:
|
294
293
|
|
295
|
-
**
|
294
|
+
**articles/_box.html.erb**
|
296
295
|
|
297
296
|
```html+erb
|
298
297
|
<div class='box'>
|
@@ -300,13 +299,13 @@ The `box` layout simply wraps the `_post` partial in a `div`:
|
|
300
299
|
</div>
|
301
300
|
```
|
302
301
|
|
303
|
-
The `
|
302
|
+
The `_article` partial wraps the article's `body` in a `div` with the `id` of the article using the `div_for` helper:
|
304
303
|
|
305
|
-
**
|
304
|
+
**articles/_article.html.erb**
|
306
305
|
|
307
306
|
```html+erb
|
308
|
-
<%= div_for(
|
309
|
-
<p><%=
|
307
|
+
<%= div_for(article) do %>
|
308
|
+
<p><%= article.body %></p>
|
310
309
|
<% end %>
|
311
310
|
```
|
312
311
|
|
@@ -314,22 +313,22 @@ this would output the following:
|
|
314
313
|
|
315
314
|
```html
|
316
315
|
<div class='box'>
|
317
|
-
<div id='
|
316
|
+
<div id='article_1'>
|
318
317
|
<p>Partial Layouts are cool!</p>
|
319
318
|
</div>
|
320
319
|
</div>
|
321
320
|
```
|
322
321
|
|
323
|
-
Note that the partial layout has access to the local `
|
322
|
+
Note that the partial layout has access to the local `article` variable that was passed into the `render` call. However, unlike application-wide layouts, partial layouts still have the underscore prefix.
|
324
323
|
|
325
|
-
You can also render a block of code within a partial layout instead of calling `yield`. For example, if we didn't have the `
|
324
|
+
You can also render a block of code within a partial layout instead of calling `yield`. For example, if we didn't have the `_article` partial, we could do this instead:
|
326
325
|
|
327
|
-
**
|
326
|
+
**articles/show.html.erb**
|
328
327
|
|
329
328
|
```html+erb
|
330
|
-
<% render(layout: 'box', locals: {
|
331
|
-
<%= div_for(
|
332
|
-
<p><%=
|
329
|
+
<% render(layout: 'box', locals: {article: @article}) do %>
|
330
|
+
<%= div_for(article) do %>
|
331
|
+
<p><%= article.body %></p>
|
333
332
|
<% end %>
|
334
333
|
<% end %>
|
335
334
|
```
|
@@ -356,18 +355,18 @@ This module provides methods for generating container tags, such as `div`, for y
|
|
356
355
|
|
357
356
|
Renders a container tag that relates to your Active Record Object.
|
358
357
|
|
359
|
-
For example, given `@
|
358
|
+
For example, given `@article` is the object of `Article` class, you can do:
|
360
359
|
|
361
360
|
```html+erb
|
362
|
-
<%= content_tag_for(:tr, @
|
363
|
-
<td><%= @
|
361
|
+
<%= content_tag_for(:tr, @article) do %>
|
362
|
+
<td><%= @article.title %></td>
|
364
363
|
<% end %>
|
365
364
|
```
|
366
365
|
|
367
366
|
This will generate this HTML output:
|
368
367
|
|
369
368
|
```html
|
370
|
-
<tr id="
|
369
|
+
<tr id="article_1234" class="article">
|
371
370
|
<td>Hello World!</td>
|
372
371
|
</tr>
|
373
372
|
```
|
@@ -375,34 +374,34 @@ This will generate this HTML output:
|
|
375
374
|
You can also supply HTML attributes as an additional option hash. For example:
|
376
375
|
|
377
376
|
```html+erb
|
378
|
-
<%= content_tag_for(:tr, @
|
379
|
-
<td><%= @
|
377
|
+
<%= content_tag_for(:tr, @article, class: "frontpage") do %>
|
378
|
+
<td><%= @article.title %></td>
|
380
379
|
<% end %>
|
381
380
|
```
|
382
381
|
|
383
382
|
Will generate this HTML output:
|
384
383
|
|
385
384
|
```html
|
386
|
-
<tr id="
|
385
|
+
<tr id="article_1234" class="article frontpage">
|
387
386
|
<td>Hello World!</td>
|
388
387
|
</tr>
|
389
388
|
```
|
390
389
|
|
391
|
-
You can pass a collection of Active Record objects. This method will loop through your objects and create a container for each of them. For example, given `@
|
390
|
+
You can pass a collection of Active Record objects. This method will loop through your objects and create a container for each of them. For example, given `@articles` is an array of two `Article` objects:
|
392
391
|
|
393
392
|
```html+erb
|
394
|
-
<%= content_tag_for(:tr, @
|
395
|
-
<td><%=
|
393
|
+
<%= content_tag_for(:tr, @articles) do |article| %>
|
394
|
+
<td><%= article.title %></td>
|
396
395
|
<% end %>
|
397
396
|
```
|
398
397
|
|
399
398
|
Will generate this HTML output:
|
400
399
|
|
401
400
|
```html
|
402
|
-
<tr id="
|
401
|
+
<tr id="article_1234" class="article">
|
403
402
|
<td>Hello World!</td>
|
404
403
|
</tr>
|
405
|
-
<tr id="
|
404
|
+
<tr id="article_1235" class="article">
|
406
405
|
<td>Ruby on Rails Rocks!</td>
|
407
406
|
</tr>
|
408
407
|
```
|
@@ -412,15 +411,15 @@ Will generate this HTML output:
|
|
412
411
|
This is actually a convenient method which calls `content_tag_for` internally with `:div` as the tag name. You can pass either an Active Record object or a collection of objects. For example:
|
413
412
|
|
414
413
|
```html+erb
|
415
|
-
<%= div_for(@
|
416
|
-
<td><%= @
|
414
|
+
<%= div_for(@article, class: "frontpage") do %>
|
415
|
+
<td><%= @article.title %></td>
|
417
416
|
<% end %>
|
418
417
|
```
|
419
418
|
|
420
419
|
Will generate this HTML output:
|
421
420
|
|
422
421
|
```html
|
423
|
-
<div id="
|
422
|
+
<div id="article_1234" class="article frontpage">
|
424
423
|
<td>Hello World!</td>
|
425
424
|
</div>
|
426
425
|
```
|
@@ -495,7 +494,7 @@ image_url("edit.png") # => http://www.example.com/assets/edit.png
|
|
495
494
|
|
496
495
|
#### image_tag
|
497
496
|
|
498
|
-
Returns an
|
497
|
+
Returns an HTML image tag for the source. The source can be a full path or a file that exists in your `app/assets/images` directory.
|
499
498
|
|
500
499
|
```ruby
|
501
500
|
image_tag("icon.png") # => <img src="/assets/icon.png" alt="Icon" />
|
@@ -503,7 +502,7 @@ image_tag("icon.png") # => <img src="/assets/icon.png" alt="Icon" />
|
|
503
502
|
|
504
503
|
#### javascript_include_tag
|
505
504
|
|
506
|
-
Returns an
|
505
|
+
Returns an HTML script tag for each of the sources provided. You can pass in the filename (`.js` extension is optional) of JavaScript files that exist in your `app/assets/javascripts` directory for inclusion into the current page or you can pass the full path relative to your document root.
|
507
506
|
|
508
507
|
```ruby
|
509
508
|
javascript_include_tag "common" # => <script src="/assets/common.js"></script>
|
@@ -590,14 +589,14 @@ This helper makes building an Atom feed easy. Here's a full usage example:
|
|
590
589
|
**config/routes.rb**
|
591
590
|
|
592
591
|
```ruby
|
593
|
-
resources :
|
592
|
+
resources :articles
|
594
593
|
```
|
595
594
|
|
596
|
-
**app/controllers/
|
595
|
+
**app/controllers/articles_controller.rb**
|
597
596
|
|
598
597
|
```ruby
|
599
598
|
def index
|
600
|
-
@
|
599
|
+
@articles = Article.all
|
601
600
|
|
602
601
|
respond_to do |format|
|
603
602
|
format.html
|
@@ -606,20 +605,20 @@ def index
|
|
606
605
|
end
|
607
606
|
```
|
608
607
|
|
609
|
-
**app/views/
|
608
|
+
**app/views/articles/index.atom.builder**
|
610
609
|
|
611
610
|
```ruby
|
612
611
|
atom_feed do |feed|
|
613
|
-
feed.title("
|
614
|
-
feed.updated((@
|
612
|
+
feed.title("Articles Index")
|
613
|
+
feed.updated((@articles.first.created_at))
|
615
614
|
|
616
|
-
@
|
617
|
-
feed.entry(
|
618
|
-
entry.title(
|
619
|
-
entry.content(
|
615
|
+
@articles.each do |article|
|
616
|
+
feed.entry(article) do |entry|
|
617
|
+
entry.title(article.title)
|
618
|
+
entry.content(article.body, type: 'html')
|
620
619
|
|
621
620
|
entry.author do |author|
|
622
|
-
author.name(
|
621
|
+
author.name(article.author_name)
|
623
622
|
end
|
624
623
|
end
|
625
624
|
end
|
@@ -697,7 +696,7 @@ For example, let's say we have a standard application layout, but also a special
|
|
697
696
|
</html>
|
698
697
|
```
|
699
698
|
|
700
|
-
**app/views/
|
699
|
+
**app/views/articles/special.html.erb**
|
701
700
|
|
702
701
|
```html+erb
|
703
702
|
<p>This is a special page.</p>
|
@@ -714,7 +713,7 @@ For example, let's say we have a standard application layout, but also a special
|
|
714
713
|
Returns a set of select tags (one for year, month, and day) pre-selected for accessing a specified date-based attribute.
|
715
714
|
|
716
715
|
```ruby
|
717
|
-
date_select("
|
716
|
+
date_select("article", "published_on")
|
718
717
|
```
|
719
718
|
|
720
719
|
#### datetime_select
|
@@ -722,7 +721,7 @@ date_select("post", "published_on")
|
|
722
721
|
Returns a set of select tags (one for year, month, day, hour, and minute) pre-selected for accessing a specified datetime-based attribute.
|
723
722
|
|
724
723
|
```ruby
|
725
|
-
datetime_select("
|
724
|
+
datetime_select("article", "published_on")
|
726
725
|
```
|
727
726
|
|
728
727
|
#### distance_of_time_in_words
|
@@ -736,7 +735,7 @@ distance_of_time_in_words(Time.now, Time.now + 15.seconds, include_seconds: true
|
|
736
735
|
|
737
736
|
#### select_date
|
738
737
|
|
739
|
-
Returns a set of
|
738
|
+
Returns a set of HTML select-tags (one for year, month, and day) pre-selected with the `date` provided.
|
740
739
|
|
741
740
|
```ruby
|
742
741
|
# Generates a date select that defaults to the date provided (six days after today)
|
@@ -748,7 +747,7 @@ select_date()
|
|
748
747
|
|
749
748
|
#### select_datetime
|
750
749
|
|
751
|
-
Returns a set of
|
750
|
+
Returns a set of HTML select-tags (one for year, month, day, hour, and minute) pre-selected with the `datetime` provided.
|
752
751
|
|
753
752
|
```ruby
|
754
753
|
# Generates a datetime select that defaults to the datetime provided (four days after today)
|
@@ -808,7 +807,7 @@ select_second(Time.now + 16.minutes)
|
|
808
807
|
|
809
808
|
#### select_time
|
810
809
|
|
811
|
-
Returns a set of
|
810
|
+
Returns a set of HTML select-tags (one for hour and minute).
|
812
811
|
|
813
812
|
```ruby
|
814
813
|
# Generates a time select that defaults to the time provided
|
@@ -904,10 +903,10 @@ The params hash has a nested person value, which can therefore be accessed with
|
|
904
903
|
Returns a checkbox tag tailored for accessing a specified attribute.
|
905
904
|
|
906
905
|
```ruby
|
907
|
-
# Let's say that @
|
908
|
-
check_box("
|
909
|
-
# => <input type="checkbox" id="
|
910
|
-
# <input name="
|
906
|
+
# Let's say that @article.validated? is 1:
|
907
|
+
check_box("article", "validated")
|
908
|
+
# => <input type="checkbox" id="article_validated" name="article[validated]" value="1" />
|
909
|
+
# <input name="article[validated]" type="hidden" value="0" />
|
911
910
|
```
|
912
911
|
|
913
912
|
#### fields_for
|
@@ -939,7 +938,7 @@ file_field(:user, :avatar)
|
|
939
938
|
Creates a form and a scope around a specific model object that is used as a base for questioning about values for the fields.
|
940
939
|
|
941
940
|
```html+erb
|
942
|
-
<%= form_for @
|
941
|
+
<%= form_for @article do |f| %>
|
943
942
|
<%= f.label :title, 'Title' %>:
|
944
943
|
<%= f.text_field :title %><br>
|
945
944
|
<%= f.label :body, 'Body' %>:
|
@@ -961,8 +960,8 @@ hidden_field(:user, :token)
|
|
961
960
|
Returns a label tag tailored for labelling an input field for a specified attribute.
|
962
961
|
|
963
962
|
```ruby
|
964
|
-
label(:
|
965
|
-
# => <label for="
|
963
|
+
label(:article, :title)
|
964
|
+
# => <label for="article_title">Title</label>
|
966
965
|
```
|
967
966
|
|
968
967
|
#### password_field
|
@@ -979,11 +978,11 @@ password_field(:login, :pass)
|
|
979
978
|
Returns a radio button tag for accessing a specified attribute.
|
980
979
|
|
981
980
|
```ruby
|
982
|
-
# Let's say that @
|
983
|
-
radio_button("
|
984
|
-
radio_button("
|
985
|
-
# => <input type="radio" id="
|
986
|
-
# <input type="radio" id="
|
981
|
+
# Let's say that @article.category returns "rails":
|
982
|
+
radio_button("article", "category", "rails")
|
983
|
+
radio_button("article", "category", "java")
|
984
|
+
# => <input type="radio" id="article_category_rails" name="article[category]" value="rails" checked="checked" />
|
985
|
+
# <input type="radio" id="article_category_java" name="article[category]" value="java" />
|
987
986
|
```
|
988
987
|
|
989
988
|
#### text_area
|
@@ -1002,8 +1001,8 @@ text_area(:comment, :text, size: "20x30")
|
|
1002
1001
|
Returns an input tag of the "text" type tailored for accessing a specified attribute.
|
1003
1002
|
|
1004
1003
|
```ruby
|
1005
|
-
text_field(:
|
1006
|
-
# => <input type="text" id="
|
1004
|
+
text_field(:article, :title)
|
1005
|
+
# => <input type="text" id="article_title" name="article[title]" value="#{@article.title}" />
|
1007
1006
|
```
|
1008
1007
|
|
1009
1008
|
#### email_field
|
@@ -1035,28 +1034,28 @@ Returns `select` and `option` tags for the collection of existing return values
|
|
1035
1034
|
Example object structure for use with this method:
|
1036
1035
|
|
1037
1036
|
```ruby
|
1038
|
-
class
|
1037
|
+
class Article < ActiveRecord::Base
|
1039
1038
|
belongs_to :author
|
1040
1039
|
end
|
1041
1040
|
|
1042
1041
|
class Author < ActiveRecord::Base
|
1043
|
-
has_many :
|
1042
|
+
has_many :articles
|
1044
1043
|
def name_with_initial
|
1045
1044
|
"#{first_name.first}. #{last_name}"
|
1046
1045
|
end
|
1047
1046
|
end
|
1048
1047
|
```
|
1049
1048
|
|
1050
|
-
Sample usage (selecting the associated Author for an instance of
|
1049
|
+
Sample usage (selecting the associated Author for an instance of Article, `@article`):
|
1051
1050
|
|
1052
1051
|
```ruby
|
1053
|
-
collection_select(:
|
1052
|
+
collection_select(:article, :author_id, Author.all, :id, :name_with_initial, {prompt: true})
|
1054
1053
|
```
|
1055
1054
|
|
1056
|
-
If `@
|
1055
|
+
If `@article.author_id` is 1, this would return:
|
1057
1056
|
|
1058
1057
|
```html
|
1059
|
-
<select name="
|
1058
|
+
<select name="article[author_id]">
|
1060
1059
|
<option value="">Please select</option>
|
1061
1060
|
<option value="1" selected="selected">D. Heinemeier Hansson</option>
|
1062
1061
|
<option value="2">D. Thomas</option>
|
@@ -1071,33 +1070,33 @@ Returns `radio_button` tags for the collection of existing return values of `met
|
|
1071
1070
|
Example object structure for use with this method:
|
1072
1071
|
|
1073
1072
|
```ruby
|
1074
|
-
class
|
1073
|
+
class Article < ActiveRecord::Base
|
1075
1074
|
belongs_to :author
|
1076
1075
|
end
|
1077
1076
|
|
1078
1077
|
class Author < ActiveRecord::Base
|
1079
|
-
has_many :
|
1078
|
+
has_many :articles
|
1080
1079
|
def name_with_initial
|
1081
1080
|
"#{first_name.first}. #{last_name}"
|
1082
1081
|
end
|
1083
1082
|
end
|
1084
1083
|
```
|
1085
1084
|
|
1086
|
-
Sample usage (selecting the associated Author for an instance of
|
1085
|
+
Sample usage (selecting the associated Author for an instance of Article, `@article`):
|
1087
1086
|
|
1088
1087
|
```ruby
|
1089
|
-
collection_radio_buttons(:
|
1088
|
+
collection_radio_buttons(:article, :author_id, Author.all, :id, :name_with_initial)
|
1090
1089
|
```
|
1091
1090
|
|
1092
|
-
If `@
|
1091
|
+
If `@article.author_id` is 1, this would return:
|
1093
1092
|
|
1094
1093
|
```html
|
1095
|
-
<input id="
|
1096
|
-
<label for="
|
1097
|
-
<input id="
|
1098
|
-
<label for="
|
1099
|
-
<input id="
|
1100
|
-
<label for="
|
1094
|
+
<input id="article_author_id_1" name="article[author_id]" type="radio" value="1" checked="checked" />
|
1095
|
+
<label for="article_author_id_1">D. Heinemeier Hansson</label>
|
1096
|
+
<input id="article_author_id_2" name="article[author_id]" type="radio" value="2" />
|
1097
|
+
<label for="article_author_id_2">D. Thomas</label>
|
1098
|
+
<input id="article_author_id_3" name="article[author_id]" type="radio" value="3" />
|
1099
|
+
<label for="article_author_id_3">M. Clark</label>
|
1101
1100
|
```
|
1102
1101
|
|
1103
1102
|
#### collection_check_boxes
|
@@ -1107,34 +1106,34 @@ Returns `check_box` tags for the collection of existing return values of `method
|
|
1107
1106
|
Example object structure for use with this method:
|
1108
1107
|
|
1109
1108
|
```ruby
|
1110
|
-
class
|
1109
|
+
class Article < ActiveRecord::Base
|
1111
1110
|
has_and_belongs_to_many :authors
|
1112
1111
|
end
|
1113
1112
|
|
1114
1113
|
class Author < ActiveRecord::Base
|
1115
|
-
has_and_belongs_to_many :
|
1114
|
+
has_and_belongs_to_many :articles
|
1116
1115
|
def name_with_initial
|
1117
1116
|
"#{first_name.first}. #{last_name}"
|
1118
1117
|
end
|
1119
1118
|
end
|
1120
1119
|
```
|
1121
1120
|
|
1122
|
-
Sample usage (selecting the associated Authors for an instance of
|
1121
|
+
Sample usage (selecting the associated Authors for an instance of Article, `@article`):
|
1123
1122
|
|
1124
1123
|
```ruby
|
1125
|
-
collection_check_boxes(:
|
1124
|
+
collection_check_boxes(:article, :author_ids, Author.all, :id, :name_with_initial)
|
1126
1125
|
```
|
1127
1126
|
|
1128
|
-
If `@
|
1127
|
+
If `@article.author_ids` is [1], this would return:
|
1129
1128
|
|
1130
1129
|
```html
|
1131
|
-
<input id="
|
1132
|
-
<label for="
|
1133
|
-
<input id="
|
1134
|
-
<label for="
|
1135
|
-
<input id="
|
1136
|
-
<label for="
|
1137
|
-
<input name="
|
1130
|
+
<input id="article_author_ids_1" name="article[author_ids][]" type="checkbox" value="1" checked="checked" />
|
1131
|
+
<label for="article_author_ids_1">D. Heinemeier Hansson</label>
|
1132
|
+
<input id="article_author_ids_2" name="article[author_ids][]" type="checkbox" value="2" />
|
1133
|
+
<label for="article_author_ids_2">D. Thomas</label>
|
1134
|
+
<input id="article_author_ids_3" name="article[author_ids][]" type="checkbox" value="3" />
|
1135
|
+
<label for="article_author_ids_3">M. Clark</label>
|
1136
|
+
<input name="article[author_ids][]" type="hidden" value="" />
|
1138
1137
|
```
|
1139
1138
|
|
1140
1139
|
#### country_options_for_select
|
@@ -1222,13 +1221,13 @@ Create a select tag and a series of contained option tags for the provided objec
|
|
1222
1221
|
Example:
|
1223
1222
|
|
1224
1223
|
```ruby
|
1225
|
-
select("
|
1224
|
+
select("article", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, {include_blank: true})
|
1226
1225
|
```
|
1227
1226
|
|
1228
|
-
If `@
|
1227
|
+
If `@article.person_id` is 1, this would become:
|
1229
1228
|
|
1230
1229
|
```html
|
1231
|
-
<select name="
|
1230
|
+
<select name="article[person_id]">
|
1232
1231
|
<option value=""></option>
|
1233
1232
|
<option value="1" selected="selected">David</option>
|
1234
1233
|
<option value="2">Sam</option>
|
@@ -1303,10 +1302,10 @@ file_field_tag 'attachment'
|
|
1303
1302
|
Starts a form tag that points the action to an url configured with `url_for_options` just like `ActionController::Base#url_for`.
|
1304
1303
|
|
1305
1304
|
```html+erb
|
1306
|
-
<%= form_tag '/
|
1305
|
+
<%= form_tag '/articles' do %>
|
1307
1306
|
<div><%= submit_tag 'Save' %></div>
|
1308
1307
|
<% end %>
|
1309
|
-
# => <form action="/
|
1308
|
+
# => <form action="/articles" method="post"><div><input type="submit" name="submit" value="Save" /></div></form>
|
1310
1309
|
```
|
1311
1310
|
|
1312
1311
|
#### hidden_field_tag
|
@@ -1368,8 +1367,8 @@ select_tag "people", "<option>David</option>"
|
|
1368
1367
|
Creates a submit button with the text provided as the caption.
|
1369
1368
|
|
1370
1369
|
```ruby
|
1371
|
-
submit_tag "Publish this
|
1372
|
-
# => <input name="commit" type="submit" value="Publish this
|
1370
|
+
submit_tag "Publish this article"
|
1371
|
+
# => <input name="commit" type="submit" value="Publish this article" />
|
1373
1372
|
```
|
1374
1373
|
|
1375
1374
|
#### text_area_tag
|
@@ -1377,8 +1376,8 @@ submit_tag "Publish this post"
|
|
1377
1376
|
Creates a text input area; use a textarea for longer text inputs such as blog posts or descriptions.
|
1378
1377
|
|
1379
1378
|
```ruby
|
1380
|
-
text_area_tag '
|
1381
|
-
# => <textarea id="
|
1379
|
+
text_area_tag 'article'
|
1380
|
+
# => <textarea id="article" name="article"></textarea>
|
1382
1381
|
```
|
1383
1382
|
|
1384
1383
|
#### text_field_tag
|
@@ -1526,7 +1525,7 @@ The SanitizeHelper module provides a set of methods for scrubbing text of undesi
|
|
1526
1525
|
|
1527
1526
|
#### sanitize
|
1528
1527
|
|
1529
|
-
This sanitize helper will
|
1528
|
+
This sanitize helper will HTML encode all tags and strip all attributes that aren't specifically allowed.
|
1530
1529
|
|
1531
1530
|
```ruby
|
1532
1531
|
sanitize @article.body
|
@@ -1550,7 +1549,7 @@ end
|
|
1550
1549
|
|
1551
1550
|
Sanitizes a block of CSS code.
|
1552
1551
|
|
1553
|
-
#### strip_links(html)
|
1552
|
+
#### strip_links(html)
|
1554
1553
|
Strips all link tags from text leaving just the link text.
|
1555
1554
|
|
1556
1555
|
```ruby
|
@@ -1568,9 +1567,9 @@ strip_links('Blog: <a href="http://myblog.com/">Visit</a>.')
|
|
1568
1567
|
# => Blog: Visit.
|
1569
1568
|
```
|
1570
1569
|
|
1571
|
-
#### strip_tags(html)
|
1570
|
+
#### strip_tags(html)
|
1572
1571
|
|
1573
|
-
Strips all HTML tags from the html, including comments.
|
1572
|
+
Strips all HTML tags from the html, including comments.
|
1574
1573
|
This uses the html-scanner tokenizer and so its HTML parsing ability is limited by that of html-scanner.
|
1575
1574
|
|
1576
1575
|
```ruby
|
@@ -1585,13 +1584,24 @@ strip_tags("<b>Bold</b> no more! <a href='more.html'>See more</a>")
|
|
1585
1584
|
|
1586
1585
|
NB: The output may still contain unescaped '<', '>', '&' characters and confuse browsers.
|
1587
1586
|
|
1587
|
+
### CsrfHelper
|
1588
|
+
|
1589
|
+
Returns meta tags "csrf-param" and "csrf-token" with the name of the cross-site
|
1590
|
+
request forgery protection parameter and token, respectively.
|
1591
|
+
|
1592
|
+
```html
|
1593
|
+
<%= csrf_meta_tags %>
|
1594
|
+
```
|
1595
|
+
|
1596
|
+
NOTE: Regular forms generate hidden fields so they do not use these tags. More
|
1597
|
+
details can be found in the [Rails Security Guide](security.html#cross-site-request-forgery-csrf).
|
1588
1598
|
|
1589
1599
|
Localized Views
|
1590
1600
|
---------------
|
1591
1601
|
|
1592
1602
|
Action View has the ability render different templates depending on the current locale.
|
1593
1603
|
|
1594
|
-
For example, suppose you have a `
|
1604
|
+
For example, suppose you have a `ArticlesController` with a show action. By default, calling this action will render `app/views/articles/show.html.erb`. But if you set `I18n.locale = :de`, then `app/views/articles/show.de.html.erb` will be rendered instead. If the localized template isn't present, the undecorated version will be used. This means you're not required to provide localized views for all cases, but they will be preferred and used if available.
|
1595
1605
|
|
1596
1606
|
You can use the same technique to localize the rescue files in your public directory. For example, setting `I18n.locale = :de` and creating `public/500.de.html` and `public/404.de.html` would allow you to have localized rescue pages.
|
1597
1607
|
|
@@ -1605,6 +1615,6 @@ def set_expert_locale
|
|
1605
1615
|
end
|
1606
1616
|
```
|
1607
1617
|
|
1608
|
-
Then you could create special views like `app/views/
|
1618
|
+
Then you could create special views like `app/views/articles/show.expert.html.erb` that would only be displayed to expert users.
|
1609
1619
|
|
1610
1620
|
You can read more about the Rails Internationalization (I18n) API [here](i18n.html).
|