rails 4.1.4 → 4.2.11.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +12 -10
- data/guides/CHANGELOG.md +87 -15
- data/guides/Rakefile +21 -6
- data/guides/assets/images/getting_started/article_with_comments.png +0 -0
- 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 +3 -3
- data/guides/bug_report_templates/action_controller_master.rb +3 -2
- data/guides/bug_report_templates/active_record_gem.rb +1 -1
- data/guides/bug_report_templates/generic_gem.rb +15 -0
- data/guides/bug_report_templates/generic_master.rb +26 -0
- data/guides/rails_guides/helpers.rb +1 -1
- data/guides/rails_guides/levenshtein.rb +27 -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 +6 -3
- 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 +877 -0
- data/guides/source/_license.html.erb +1 -1
- data/guides/source/_welcome.html.erb +6 -8
- data/guides/source/action_controller_overview.md +28 -11
- data/guides/source/action_mailer_basics.md +97 -29
- data/guides/source/action_view_overview.md +142 -191
- data/guides/source/active_job_basics.md +339 -0
- data/guides/source/active_model_basics.md +371 -17
- data/guides/source/active_record_basics.md +25 -24
- 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 +270 -262
- data/guides/source/active_record_validations.md +24 -14
- data/guides/source/active_support_core_extensions.md +115 -123
- data/guides/source/active_support_instrumentation.md +10 -18
- data/guides/source/api_documentation_guidelines.md +63 -17
- data/guides/source/asset_pipeline.md +266 -125
- data/guides/source/association_basics.md +96 -80
- data/guides/source/autoloading_and_reloading_constants.md +1311 -0
- data/guides/source/caching_with_rails.md +32 -7
- data/guides/source/command_line.md +52 -30
- data/guides/source/configuring.md +161 -33
- data/guides/source/contributing_to_ruby_on_rails.md +198 -114
- 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 +217 -196
- data/guides/source/form_helpers.md +79 -56
- data/guides/source/generators.md +24 -11
- data/guides/source/getting_started.md +361 -222
- data/guides/source/i18n.md +113 -69
- data/guides/source/index.html.erb +1 -0
- data/guides/source/initialization.md +110 -63
- data/guides/source/layout.html.erb +5 -11
- data/guides/source/layouts_and_rendering.md +26 -26
- 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 -10
- data/guides/source/routing.md +115 -75
- data/guides/source/ruby_on_rails_guides_guidelines.md +11 -12
- data/guides/source/security.md +41 -35
- data/guides/source/testing.md +199 -119
- data/guides/source/upgrading_ruby_on_rails.md +319 -32
- data/guides/source/working_with_javascript_in_rails.md +19 -17
- data/guides/w3c_validator.rb +2 -0
- metadata +42 -95
- 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
|
```
|
@@ -436,39 +435,13 @@ config.action_controller.asset_host = "assets.example.com"
|
|
436
435
|
image_tag("rails.png") # => <img src="http://assets.example.com/images/rails.png" alt="Rails" />
|
437
436
|
```
|
438
437
|
|
439
|
-
#### register_javascript_expansion
|
440
|
-
|
441
|
-
Register one or more JavaScript files to be included when symbol is passed to javascript_include_tag. This method is typically intended to be called from plugin initialization to register JavaScript files that the plugin installed in `vendor/assets/javascripts`.
|
442
|
-
|
443
|
-
```ruby
|
444
|
-
ActionView::Helpers::AssetTagHelper.register_javascript_expansion monkey: ["head", "body", "tail"]
|
445
|
-
|
446
|
-
javascript_include_tag :monkey # =>
|
447
|
-
<script src="/assets/head.js"></script>
|
448
|
-
<script src="/assets/body.js"></script>
|
449
|
-
<script src="/assets/tail.js"></script>
|
450
|
-
```
|
451
|
-
|
452
|
-
#### register_stylesheet_expansion
|
453
|
-
|
454
|
-
Register one or more stylesheet files to be included when symbol is passed to `stylesheet_link_tag`. This method is typically intended to be called from plugin initialization to register stylesheet files that the plugin installed in `vendor/assets/stylesheets`.
|
455
|
-
|
456
|
-
```ruby
|
457
|
-
ActionView::Helpers::AssetTagHelper.register_stylesheet_expansion monkey: ["head", "body", "tail"]
|
458
|
-
|
459
|
-
stylesheet_link_tag :monkey # =>
|
460
|
-
<link href="/assets/head.css" media="screen" rel="stylesheet" />
|
461
|
-
<link href="/assets/body.css" media="screen" rel="stylesheet" />
|
462
|
-
<link href="/assets/tail.css" media="screen" rel="stylesheet" />
|
463
|
-
```
|
464
|
-
|
465
438
|
#### auto_discovery_link_tag
|
466
439
|
|
467
440
|
Returns a link tag that browsers and feed readers can use to auto-detect an RSS or Atom feed.
|
468
441
|
|
469
442
|
```ruby
|
470
|
-
auto_discovery_link_tag(:rss, "http://www.example.com/feed.rss", {title: "RSS Feed"}) # =>
|
471
|
-
<link rel="alternate" type="application/rss+xml" title="RSS Feed" href="http://www.example.com/feed" />
|
443
|
+
auto_discovery_link_tag(:rss, "http://www.example.com/feed.rss", { title: "RSS Feed" }) # =>
|
444
|
+
<link rel="alternate" type="application/rss+xml" title="RSS Feed" href="http://www.example.com/feed.rss" />
|
472
445
|
```
|
473
446
|
|
474
447
|
#### image_path
|
@@ -495,7 +468,7 @@ image_url("edit.png") # => http://www.example.com/assets/edit.png
|
|
495
468
|
|
496
469
|
#### image_tag
|
497
470
|
|
498
|
-
Returns an
|
471
|
+
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
472
|
|
500
473
|
```ruby
|
501
474
|
image_tag("icon.png") # => <img src="/assets/icon.png" alt="Icon" />
|
@@ -503,7 +476,7 @@ image_tag("icon.png") # => <img src="/assets/icon.png" alt="Icon" />
|
|
503
476
|
|
504
477
|
#### javascript_include_tag
|
505
478
|
|
506
|
-
Returns an
|
479
|
+
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
480
|
|
508
481
|
```ruby
|
509
482
|
javascript_include_tag "common" # => <script src="/assets/common.js"></script>
|
@@ -590,14 +563,14 @@ This helper makes building an Atom feed easy. Here's a full usage example:
|
|
590
563
|
**config/routes.rb**
|
591
564
|
|
592
565
|
```ruby
|
593
|
-
resources :
|
566
|
+
resources :articles
|
594
567
|
```
|
595
568
|
|
596
|
-
**app/controllers/
|
569
|
+
**app/controllers/articles_controller.rb**
|
597
570
|
|
598
571
|
```ruby
|
599
572
|
def index
|
600
|
-
@
|
573
|
+
@articles = Article.all
|
601
574
|
|
602
575
|
respond_to do |format|
|
603
576
|
format.html
|
@@ -606,20 +579,20 @@ def index
|
|
606
579
|
end
|
607
580
|
```
|
608
581
|
|
609
|
-
**app/views/
|
582
|
+
**app/views/articles/index.atom.builder**
|
610
583
|
|
611
584
|
```ruby
|
612
585
|
atom_feed do |feed|
|
613
|
-
feed.title("
|
614
|
-
feed.updated((@
|
586
|
+
feed.title("Articles Index")
|
587
|
+
feed.updated((@articles.first.created_at))
|
615
588
|
|
616
|
-
@
|
617
|
-
feed.entry(
|
618
|
-
entry.title(
|
619
|
-
entry.content(
|
589
|
+
@articles.each do |article|
|
590
|
+
feed.entry(article) do |entry|
|
591
|
+
entry.title(article.title)
|
592
|
+
entry.content(article.body, type: 'html')
|
620
593
|
|
621
594
|
entry.author do |author|
|
622
|
-
author.name(
|
595
|
+
author.name(article.author_name)
|
623
596
|
end
|
624
597
|
end
|
625
598
|
end
|
@@ -697,7 +670,7 @@ For example, let's say we have a standard application layout, but also a special
|
|
697
670
|
</html>
|
698
671
|
```
|
699
672
|
|
700
|
-
**app/views/
|
673
|
+
**app/views/articles/special.html.erb**
|
701
674
|
|
702
675
|
```html+erb
|
703
676
|
<p>This is a special page.</p>
|
@@ -714,7 +687,7 @@ For example, let's say we have a standard application layout, but also a special
|
|
714
687
|
Returns a set of select tags (one for year, month, and day) pre-selected for accessing a specified date-based attribute.
|
715
688
|
|
716
689
|
```ruby
|
717
|
-
date_select("
|
690
|
+
date_select("article", "published_on")
|
718
691
|
```
|
719
692
|
|
720
693
|
#### datetime_select
|
@@ -722,7 +695,7 @@ date_select("post", "published_on")
|
|
722
695
|
Returns a set of select tags (one for year, month, day, hour, and minute) pre-selected for accessing a specified datetime-based attribute.
|
723
696
|
|
724
697
|
```ruby
|
725
|
-
datetime_select("
|
698
|
+
datetime_select("article", "published_on")
|
726
699
|
```
|
727
700
|
|
728
701
|
#### distance_of_time_in_words
|
@@ -736,7 +709,7 @@ distance_of_time_in_words(Time.now, Time.now + 15.seconds, include_seconds: true
|
|
736
709
|
|
737
710
|
#### select_date
|
738
711
|
|
739
|
-
Returns a set of
|
712
|
+
Returns a set of HTML select-tags (one for year, month, and day) pre-selected with the `date` provided.
|
740
713
|
|
741
714
|
```ruby
|
742
715
|
# Generates a date select that defaults to the date provided (six days after today)
|
@@ -748,7 +721,7 @@ select_date()
|
|
748
721
|
|
749
722
|
#### select_datetime
|
750
723
|
|
751
|
-
Returns a set of
|
724
|
+
Returns a set of HTML select-tags (one for year, month, day, hour, and minute) pre-selected with the `datetime` provided.
|
752
725
|
|
753
726
|
```ruby
|
754
727
|
# Generates a datetime select that defaults to the datetime provided (four days after today)
|
@@ -808,7 +781,7 @@ select_second(Time.now + 16.minutes)
|
|
808
781
|
|
809
782
|
#### select_time
|
810
783
|
|
811
|
-
Returns a set of
|
784
|
+
Returns a set of HTML select-tags (one for hour and minute).
|
812
785
|
|
813
786
|
```ruby
|
814
787
|
# Generates a time select that defaults to the time provided
|
@@ -904,10 +877,10 @@ The params hash has a nested person value, which can therefore be accessed with
|
|
904
877
|
Returns a checkbox tag tailored for accessing a specified attribute.
|
905
878
|
|
906
879
|
```ruby
|
907
|
-
# Let's say that @
|
908
|
-
check_box("
|
909
|
-
# => <input type="checkbox" id="
|
910
|
-
# <input name="
|
880
|
+
# Let's say that @article.validated? is 1:
|
881
|
+
check_box("article", "validated")
|
882
|
+
# => <input type="checkbox" id="article_validated" name="article[validated]" value="1" />
|
883
|
+
# <input name="article[validated]" type="hidden" value="0" />
|
911
884
|
```
|
912
885
|
|
913
886
|
#### fields_for
|
@@ -939,7 +912,7 @@ file_field(:user, :avatar)
|
|
939
912
|
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
913
|
|
941
914
|
```html+erb
|
942
|
-
<%= form_for @
|
915
|
+
<%= form_for @article do |f| %>
|
943
916
|
<%= f.label :title, 'Title' %>:
|
944
917
|
<%= f.text_field :title %><br>
|
945
918
|
<%= f.label :body, 'Body' %>:
|
@@ -961,8 +934,8 @@ hidden_field(:user, :token)
|
|
961
934
|
Returns a label tag tailored for labelling an input field for a specified attribute.
|
962
935
|
|
963
936
|
```ruby
|
964
|
-
label(:
|
965
|
-
# => <label for="
|
937
|
+
label(:article, :title)
|
938
|
+
# => <label for="article_title">Title</label>
|
966
939
|
```
|
967
940
|
|
968
941
|
#### password_field
|
@@ -979,11 +952,11 @@ password_field(:login, :pass)
|
|
979
952
|
Returns a radio button tag for accessing a specified attribute.
|
980
953
|
|
981
954
|
```ruby
|
982
|
-
# Let's say that @
|
983
|
-
radio_button("
|
984
|
-
radio_button("
|
985
|
-
# => <input type="radio" id="
|
986
|
-
# <input type="radio" id="
|
955
|
+
# Let's say that @article.category returns "rails":
|
956
|
+
radio_button("article", "category", "rails")
|
957
|
+
radio_button("article", "category", "java")
|
958
|
+
# => <input type="radio" id="article_category_rails" name="article[category]" value="rails" checked="checked" />
|
959
|
+
# <input type="radio" id="article_category_java" name="article[category]" value="java" />
|
987
960
|
```
|
988
961
|
|
989
962
|
#### text_area
|
@@ -1002,8 +975,8 @@ text_area(:comment, :text, size: "20x30")
|
|
1002
975
|
Returns an input tag of the "text" type tailored for accessing a specified attribute.
|
1003
976
|
|
1004
977
|
```ruby
|
1005
|
-
text_field(:
|
1006
|
-
# => <input type="text" id="
|
978
|
+
text_field(:article, :title)
|
979
|
+
# => <input type="text" id="article_title" name="article[title]" value="#{@article.title}" />
|
1007
980
|
```
|
1008
981
|
|
1009
982
|
#### email_field
|
@@ -1035,28 +1008,28 @@ Returns `select` and `option` tags for the collection of existing return values
|
|
1035
1008
|
Example object structure for use with this method:
|
1036
1009
|
|
1037
1010
|
```ruby
|
1038
|
-
class
|
1011
|
+
class Article < ActiveRecord::Base
|
1039
1012
|
belongs_to :author
|
1040
1013
|
end
|
1041
1014
|
|
1042
1015
|
class Author < ActiveRecord::Base
|
1043
|
-
has_many :
|
1016
|
+
has_many :articles
|
1044
1017
|
def name_with_initial
|
1045
1018
|
"#{first_name.first}. #{last_name}"
|
1046
1019
|
end
|
1047
1020
|
end
|
1048
1021
|
```
|
1049
1022
|
|
1050
|
-
Sample usage (selecting the associated Author for an instance of
|
1023
|
+
Sample usage (selecting the associated Author for an instance of Article, `@article`):
|
1051
1024
|
|
1052
1025
|
```ruby
|
1053
|
-
collection_select(:
|
1026
|
+
collection_select(:article, :author_id, Author.all, :id, :name_with_initial, {prompt: true})
|
1054
1027
|
```
|
1055
1028
|
|
1056
|
-
If `@
|
1029
|
+
If `@article.author_id` is 1, this would return:
|
1057
1030
|
|
1058
1031
|
```html
|
1059
|
-
<select name="
|
1032
|
+
<select name="article[author_id]">
|
1060
1033
|
<option value="">Please select</option>
|
1061
1034
|
<option value="1" selected="selected">D. Heinemeier Hansson</option>
|
1062
1035
|
<option value="2">D. Thomas</option>
|
@@ -1071,33 +1044,33 @@ Returns `radio_button` tags for the collection of existing return values of `met
|
|
1071
1044
|
Example object structure for use with this method:
|
1072
1045
|
|
1073
1046
|
```ruby
|
1074
|
-
class
|
1047
|
+
class Article < ActiveRecord::Base
|
1075
1048
|
belongs_to :author
|
1076
1049
|
end
|
1077
1050
|
|
1078
1051
|
class Author < ActiveRecord::Base
|
1079
|
-
has_many :
|
1052
|
+
has_many :articles
|
1080
1053
|
def name_with_initial
|
1081
1054
|
"#{first_name.first}. #{last_name}"
|
1082
1055
|
end
|
1083
1056
|
end
|
1084
1057
|
```
|
1085
1058
|
|
1086
|
-
Sample usage (selecting the associated Author for an instance of
|
1059
|
+
Sample usage (selecting the associated Author for an instance of Article, `@article`):
|
1087
1060
|
|
1088
1061
|
```ruby
|
1089
|
-
collection_radio_buttons(:
|
1062
|
+
collection_radio_buttons(:article, :author_id, Author.all, :id, :name_with_initial)
|
1090
1063
|
```
|
1091
1064
|
|
1092
|
-
If `@
|
1065
|
+
If `@article.author_id` is 1, this would return:
|
1093
1066
|
|
1094
1067
|
```html
|
1095
|
-
<input id="
|
1096
|
-
<label for="
|
1097
|
-
<input id="
|
1098
|
-
<label for="
|
1099
|
-
<input id="
|
1100
|
-
<label for="
|
1068
|
+
<input id="article_author_id_1" name="article[author_id]" type="radio" value="1" checked="checked" />
|
1069
|
+
<label for="article_author_id_1">D. Heinemeier Hansson</label>
|
1070
|
+
<input id="article_author_id_2" name="article[author_id]" type="radio" value="2" />
|
1071
|
+
<label for="article_author_id_2">D. Thomas</label>
|
1072
|
+
<input id="article_author_id_3" name="article[author_id]" type="radio" value="3" />
|
1073
|
+
<label for="article_author_id_3">M. Clark</label>
|
1101
1074
|
```
|
1102
1075
|
|
1103
1076
|
#### collection_check_boxes
|
@@ -1107,44 +1080,36 @@ Returns `check_box` tags for the collection of existing return values of `method
|
|
1107
1080
|
Example object structure for use with this method:
|
1108
1081
|
|
1109
1082
|
```ruby
|
1110
|
-
class
|
1083
|
+
class Article < ActiveRecord::Base
|
1111
1084
|
has_and_belongs_to_many :authors
|
1112
1085
|
end
|
1113
1086
|
|
1114
1087
|
class Author < ActiveRecord::Base
|
1115
|
-
has_and_belongs_to_many :
|
1088
|
+
has_and_belongs_to_many :articles
|
1116
1089
|
def name_with_initial
|
1117
1090
|
"#{first_name.first}. #{last_name}"
|
1118
1091
|
end
|
1119
1092
|
end
|
1120
1093
|
```
|
1121
1094
|
|
1122
|
-
Sample usage (selecting the associated Authors for an instance of
|
1095
|
+
Sample usage (selecting the associated Authors for an instance of Article, `@article`):
|
1123
1096
|
|
1124
1097
|
```ruby
|
1125
|
-
collection_check_boxes(:
|
1098
|
+
collection_check_boxes(:article, :author_ids, Author.all, :id, :name_with_initial)
|
1126
1099
|
```
|
1127
1100
|
|
1128
|
-
If `@
|
1101
|
+
If `@article.author_ids` is [1], this would return:
|
1129
1102
|
|
1130
1103
|
```html
|
1131
|
-
<input id="
|
1132
|
-
<label for="
|
1133
|
-
<input id="
|
1134
|
-
<label for="
|
1135
|
-
<input id="
|
1136
|
-
<label for="
|
1137
|
-
<input name="
|
1104
|
+
<input id="article_author_ids_1" name="article[author_ids][]" type="checkbox" value="1" checked="checked" />
|
1105
|
+
<label for="article_author_ids_1">D. Heinemeier Hansson</label>
|
1106
|
+
<input id="article_author_ids_2" name="article[author_ids][]" type="checkbox" value="2" />
|
1107
|
+
<label for="article_author_ids_2">D. Thomas</label>
|
1108
|
+
<input id="article_author_ids_3" name="article[author_ids][]" type="checkbox" value="3" />
|
1109
|
+
<label for="article_author_ids_3">M. Clark</label>
|
1110
|
+
<input name="article[author_ids][]" type="hidden" value="" />
|
1138
1111
|
```
|
1139
1112
|
|
1140
|
-
#### country_options_for_select
|
1141
|
-
|
1142
|
-
Returns a string of option tags for pretty much any country in the world.
|
1143
|
-
|
1144
|
-
#### country_select
|
1145
|
-
|
1146
|
-
Returns select and option tags for the given object and method, using country_options_for_select to generate the list of option tags.
|
1147
|
-
|
1148
1113
|
#### option_groups_from_collection_for_select
|
1149
1114
|
|
1150
1115
|
Returns a string of `option` tags, like `options_from_collection_for_select`, but groups them by `optgroup` tags based on the object relationships of the arguments.
|
@@ -1222,13 +1187,13 @@ Create a select tag and a series of contained option tags for the provided objec
|
|
1222
1187
|
Example:
|
1223
1188
|
|
1224
1189
|
```ruby
|
1225
|
-
select("
|
1190
|
+
select("article", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, {include_blank: true})
|
1226
1191
|
```
|
1227
1192
|
|
1228
|
-
If `@
|
1193
|
+
If `@article.person_id` is 1, this would become:
|
1229
1194
|
|
1230
1195
|
```html
|
1231
|
-
<select name="
|
1196
|
+
<select name="article[person_id]">
|
1232
1197
|
<option value=""></option>
|
1233
1198
|
<option value="1" selected="selected">David</option>
|
1234
1199
|
<option value="2">Sam</option>
|
@@ -1303,10 +1268,10 @@ file_field_tag 'attachment'
|
|
1303
1268
|
Starts a form tag that points the action to an url configured with `url_for_options` just like `ActionController::Base#url_for`.
|
1304
1269
|
|
1305
1270
|
```html+erb
|
1306
|
-
<%= form_tag '/
|
1271
|
+
<%= form_tag '/articles' do %>
|
1307
1272
|
<div><%= submit_tag 'Save' %></div>
|
1308
1273
|
<% end %>
|
1309
|
-
# => <form action="/
|
1274
|
+
# => <form action="/articles" method="post"><div><input type="submit" name="submit" value="Save" /></div></form>
|
1310
1275
|
```
|
1311
1276
|
|
1312
1277
|
#### hidden_field_tag
|
@@ -1368,8 +1333,8 @@ select_tag "people", "<option>David</option>"
|
|
1368
1333
|
Creates a submit button with the text provided as the caption.
|
1369
1334
|
|
1370
1335
|
```ruby
|
1371
|
-
submit_tag "Publish this
|
1372
|
-
# => <input name="commit" type="submit" value="Publish this
|
1336
|
+
submit_tag "Publish this article"
|
1337
|
+
# => <input name="commit" type="submit" value="Publish this article" />
|
1373
1338
|
```
|
1374
1339
|
|
1375
1340
|
#### text_area_tag
|
@@ -1377,8 +1342,8 @@ submit_tag "Publish this post"
|
|
1377
1342
|
Creates a text input area; use a textarea for longer text inputs such as blog posts or descriptions.
|
1378
1343
|
|
1379
1344
|
```ruby
|
1380
|
-
text_area_tag '
|
1381
|
-
# => <textarea id="
|
1345
|
+
text_area_tag 'article'
|
1346
|
+
# => <textarea id="article" name="article"></textarea>
|
1382
1347
|
```
|
1383
1348
|
|
1384
1349
|
#### text_field_tag
|
@@ -1421,22 +1386,6 @@ date_field_tag "dob"
|
|
1421
1386
|
|
1422
1387
|
Provides functionality for working with JavaScript in your views.
|
1423
1388
|
|
1424
|
-
#### button_to_function
|
1425
|
-
|
1426
|
-
Returns a button that'll trigger a JavaScript function using the onclick handler. Examples:
|
1427
|
-
|
1428
|
-
```ruby
|
1429
|
-
button_to_function "Greeting", "alert('Hello world!')"
|
1430
|
-
button_to_function "Delete", "if (confirm('Really?')) do_delete()"
|
1431
|
-
button_to_function "Details" do |page|
|
1432
|
-
page[:details].visual_effect :toggle_slide
|
1433
|
-
end
|
1434
|
-
```
|
1435
|
-
|
1436
|
-
#### define_javascript_functions
|
1437
|
-
|
1438
|
-
Includes the Action Pack JavaScript libraries inside a single `script` tag.
|
1439
|
-
|
1440
1389
|
#### escape_javascript
|
1441
1390
|
|
1442
1391
|
Escape carrier returns and single and double quotes for JavaScript segments.
|
@@ -1457,15 +1406,6 @@ alert('All is good')
|
|
1457
1406
|
</script>
|
1458
1407
|
```
|
1459
1408
|
|
1460
|
-
#### link_to_function
|
1461
|
-
|
1462
|
-
Returns a link that will trigger a JavaScript function using the onclick handler and return false after the fact.
|
1463
|
-
|
1464
|
-
```ruby
|
1465
|
-
link_to_function "Greeting", "alert('Hello world!')"
|
1466
|
-
# => <a onclick="alert('Hello world!'); return false;" href="#">Greeting</a>
|
1467
|
-
```
|
1468
|
-
|
1469
1409
|
### NumberHelper
|
1470
1410
|
|
1471
1411
|
Provides methods for converting numbers into formatted strings. Methods are provided for phone numbers, currency, percentage, precision, positional notation, and file size.
|
@@ -1526,7 +1466,7 @@ The SanitizeHelper module provides a set of methods for scrubbing text of undesi
|
|
1526
1466
|
|
1527
1467
|
#### sanitize
|
1528
1468
|
|
1529
|
-
This sanitize helper will
|
1469
|
+
This sanitize helper will HTML encode all tags and strip all attributes that aren't specifically allowed.
|
1530
1470
|
|
1531
1471
|
```ruby
|
1532
1472
|
sanitize @article.body
|
@@ -1550,7 +1490,7 @@ end
|
|
1550
1490
|
|
1551
1491
|
Sanitizes a block of CSS code.
|
1552
1492
|
|
1553
|
-
#### strip_links(html)
|
1493
|
+
#### strip_links(html)
|
1554
1494
|
Strips all link tags from text leaving just the link text.
|
1555
1495
|
|
1556
1496
|
```ruby
|
@@ -1568,9 +1508,9 @@ strip_links('Blog: <a href="http://myblog.com/">Visit</a>.')
|
|
1568
1508
|
# => Blog: Visit.
|
1569
1509
|
```
|
1570
1510
|
|
1571
|
-
#### strip_tags(html)
|
1511
|
+
#### strip_tags(html)
|
1572
1512
|
|
1573
|
-
Strips all HTML tags from the html, including comments.
|
1513
|
+
Strips all HTML tags from the html, including comments.
|
1574
1514
|
This uses the html-scanner tokenizer and so its HTML parsing ability is limited by that of html-scanner.
|
1575
1515
|
|
1576
1516
|
```ruby
|
@@ -1585,13 +1525,24 @@ strip_tags("<b>Bold</b> no more! <a href='more.html'>See more</a>")
|
|
1585
1525
|
|
1586
1526
|
NB: The output may still contain unescaped '<', '>', '&' characters and confuse browsers.
|
1587
1527
|
|
1528
|
+
### CsrfHelper
|
1529
|
+
|
1530
|
+
Returns meta tags "csrf-param" and "csrf-token" with the name of the cross-site
|
1531
|
+
request forgery protection parameter and token, respectively.
|
1532
|
+
|
1533
|
+
```html
|
1534
|
+
<%= csrf_meta_tags %>
|
1535
|
+
```
|
1536
|
+
|
1537
|
+
NOTE: Regular forms generate hidden fields so they do not use these tags. More
|
1538
|
+
details can be found in the [Rails Security Guide](security.html#cross-site-request-forgery-csrf).
|
1588
1539
|
|
1589
1540
|
Localized Views
|
1590
1541
|
---------------
|
1591
1542
|
|
1592
1543
|
Action View has the ability render different templates depending on the current locale.
|
1593
1544
|
|
1594
|
-
For example, suppose you have a `
|
1545
|
+
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
1546
|
|
1596
1547
|
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
1548
|
|
@@ -1605,6 +1556,6 @@ def set_expert_locale
|
|
1605
1556
|
end
|
1606
1557
|
```
|
1607
1558
|
|
1608
|
-
Then you could create special views like `app/views/
|
1559
|
+
Then you could create special views like `app/views/articles/show.expert.html.erb` that would only be displayed to expert users.
|
1609
1560
|
|
1610
1561
|
You can read more about the Rails Internationalization (I18n) API [here](i18n.html).
|