rails 4.1.16 → 4.2.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -3
- data/guides/CHANGELOG.md +13 -102
- data/guides/Rakefile +2 -2
- 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 +5 -2
- data/guides/bug_report_templates/active_record_master.rb +2 -0
- data/guides/rails_guides.rb +2 -2
- data/guides/rails_guides/helpers.rb +1 -1
- data/guides/rails_guides/levenshtein.rb +29 -21
- data/guides/rails_guides/markdown.rb +6 -7
- data/guides/rails_guides/markdown/renderer.rb +1 -1
- data/guides/source/2_3_release_notes.md +3 -3
- data/guides/source/3_0_release_notes.md +4 -4
- data/guides/source/3_1_release_notes.md +2 -2
- data/guides/source/3_2_release_notes.md +2 -2
- data/guides/source/4_1_release_notes.md +8 -9
- data/guides/source/4_2_release_notes.md +572 -0
- data/guides/source/_license.html.erb +1 -1
- data/guides/source/_welcome.html.erb +2 -8
- data/guides/source/action_controller_overview.md +79 -7
- data/guides/source/action_mailer_basics.md +36 -11
- data/guides/source/action_view_overview.md +138 -119
- data/guides/source/active_job_basics.md +253 -0
- data/guides/source/active_model_basics.md +23 -0
- data/guides/source/active_record_basics.md +16 -15
- data/guides/source/active_record_callbacks.md +12 -9
- data/guides/source/{migrations.md → active_record_migrations.md} +90 -217
- data/guides/source/active_record_postgresql.md +437 -0
- data/guides/source/active_record_querying.md +261 -261
- data/guides/source/active_record_validations.md +7 -7
- data/guides/source/active_support_core_extensions.md +105 -44
- data/guides/source/active_support_instrumentation.md +3 -2
- data/guides/source/api_documentation_guidelines.md +62 -16
- data/guides/source/asset_pipeline.md +58 -46
- data/guides/source/association_basics.md +47 -38
- data/guides/source/caching_with_rails.md +31 -6
- data/guides/source/command_line.md +56 -25
- data/guides/source/configuring.md +98 -19
- data/guides/source/contributing_to_ruby_on_rails.md +174 -111
- data/guides/source/credits.html.erb +1 -1
- data/guides/source/debugging_rails_applications.md +438 -284
- data/guides/source/development_dependencies_install.md +17 -4
- data/guides/source/documents.yaml +11 -7
- data/guides/source/engines.md +192 -203
- data/guides/source/form_helpers.md +54 -45
- data/guides/source/generators.md +20 -11
- data/guides/source/getting_started.md +330 -191
- data/guides/source/i18n.md +92 -62
- data/guides/source/index.html.erb +1 -0
- data/guides/source/initialization.md +108 -59
- data/guides/source/layout.html.erb +1 -4
- data/guides/source/layouts_and_rendering.md +24 -23
- data/guides/source/nested_model_forms.md +3 -3
- data/guides/source/plugins.md +26 -26
- data/guides/source/rails_application_templates.md +21 -3
- data/guides/source/rails_on_rack.md +1 -1
- data/guides/source/routing.md +97 -71
- data/guides/source/ruby_on_rails_guides_guidelines.md +10 -12
- data/guides/source/security.md +39 -33
- data/guides/source/testing.md +111 -108
- data/guides/source/upgrading_ruby_on_rails.md +131 -14
- data/guides/source/working_with_javascript_in_rails.md +18 -16
- data/guides/w3c_validator.rb +2 -0
- metadata +37 -94
- data/guides/bug_report_templates/generic_gem.rb +0 -15
- data/guides/bug_report_templates/generic_master.rb +0 -26
- 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.ru +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/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
@@ -1,2 +1,2 @@
|
|
1
|
-
<p>This work is licensed under a <a href="
|
1
|
+
<p>This work is licensed under a <a href="https://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International</a> License</p>
|
2
2
|
<p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
|
@@ -10,16 +10,10 @@
|
|
10
10
|
</p>
|
11
11
|
<% else %>
|
12
12
|
<p>
|
13
|
-
These are the new guides for Rails 4.
|
13
|
+
These are the new guides for Rails 4.2 based on <a href="https://github.com/rails/rails/tree/<%= @version %>"><%= @version %></a>.
|
14
14
|
These guides are designed to make you immediately productive with Rails, and to help you understand how all of the pieces fit together.
|
15
15
|
</p>
|
16
16
|
<% end %>
|
17
17
|
<p>
|
18
|
-
The guides for Rails 4.
|
19
|
-
</p>
|
20
|
-
<p>
|
21
|
-
The guides for Rails 3.2.x are available at <a href="http://guides.rubyonrails.org/v3.2.19/">http://guides.rubyonrails.org/v3.2.19/</a>.
|
22
|
-
</p>
|
23
|
-
<p>
|
24
|
-
The guides for Rails 2.3.x are available at <a href="http://guides.rubyonrails.org/v2.3.11/">http://guides.rubyonrails.org/v2.3.11/</a>.
|
18
|
+
The guides for earlier releases: <a href="http://guides.rubyonrails.org/v4.1.4/">Rails 4.1.4</a>, <a href="http://guides.rubyonrails.org/v4.0.8/">Rails 4.0.8</a>, <a href="http://guides.rubyonrails.org/v3.2.19/">Rails 3.2.19</a> and <a href="http://guides.rubyonrails.org/v2.3.11/">Rails 2.3.11</a>.
|
25
19
|
</p>
|
@@ -34,7 +34,7 @@ The naming convention of controllers in Rails favors pluralization of the last w
|
|
34
34
|
|
35
35
|
Following this convention will allow you to use the default route generators (e.g. `resources`, etc) without needing to qualify each `:path` or `:controller`, and keeps URL and path helpers' usage consistent throughout your application. See [Layouts & Rendering Guide](layouts_and_rendering.html) for more details.
|
36
36
|
|
37
|
-
NOTE: The controller naming convention differs from the naming convention of models, which expected to be named in singular form.
|
37
|
+
NOTE: The controller naming convention differs from the naming convention of models, which are expected to be named in singular form.
|
38
38
|
|
39
39
|
|
40
40
|
Methods and Actions
|
@@ -260,7 +260,7 @@ used:
|
|
260
260
|
params.require(:log_entry).permit!
|
261
261
|
```
|
262
262
|
|
263
|
-
This will mark the `:log_entry` parameters hash and any
|
263
|
+
This will mark the `:log_entry` parameters hash and any sub-hash of it
|
264
264
|
permitted. Extreme care should be taken when using `permit!` as it
|
265
265
|
will allow all current and future model attributes to be
|
266
266
|
mass-assigned.
|
@@ -381,16 +381,31 @@ You can also pass a `:domain` key and specify the domain name for the cookie:
|
|
381
381
|
Rails.application.config.session_store :cookie_store, key: '_your_app_session', domain: ".example.com"
|
382
382
|
```
|
383
383
|
|
384
|
-
Rails sets up (for the CookieStore) a secret key used for signing the session data. This can be changed in `config/
|
384
|
+
Rails sets up (for the CookieStore) a secret key used for signing the session data. This can be changed in `config/secrets.yml`
|
385
385
|
|
386
386
|
```ruby
|
387
387
|
# Be sure to restart your server when you modify this file.
|
388
388
|
|
389
|
-
# Your secret key for verifying the integrity of signed cookies.
|
389
|
+
# Your secret key is used for verifying the integrity of signed cookies.
|
390
390
|
# If you change this key, all old signed cookies will become invalid!
|
391
|
+
|
391
392
|
# Make sure the secret is at least 30 characters and all random,
|
392
393
|
# no regular words or you'll be exposed to dictionary attacks.
|
393
|
-
|
394
|
+
# You can use `rake secret` to generate a secure secret key.
|
395
|
+
|
396
|
+
# Make sure the secrets in this file are kept private
|
397
|
+
# if you're sharing your code publicly.
|
398
|
+
|
399
|
+
development:
|
400
|
+
secret_key_base: a75d...
|
401
|
+
|
402
|
+
test:
|
403
|
+
secret_key_base: 492f...
|
404
|
+
|
405
|
+
# Do not keep production secrets in the repository,
|
406
|
+
# instead read values from the environment.
|
407
|
+
production:
|
408
|
+
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
|
394
409
|
```
|
395
410
|
|
396
411
|
NOTE: Changing the secret when using the `CookieStore` will invalidate all existing sessions.
|
@@ -1063,7 +1078,7 @@ Rails keeps a log file for each environment in the `log` folder. These are extre
|
|
1063
1078
|
|
1064
1079
|
### Parameters Filtering
|
1065
1080
|
|
1066
|
-
You can filter
|
1081
|
+
You can filter out sensitive request parameters from your log files by appending them to `config.filter_parameters` in the application configuration. These parameters will be marked [FILTERED] in the log.
|
1067
1082
|
|
1068
1083
|
```ruby
|
1069
1084
|
config.filter_parameters << :password
|
@@ -1071,7 +1086,7 @@ config.filter_parameters << :password
|
|
1071
1086
|
|
1072
1087
|
### Redirects Filtering
|
1073
1088
|
|
1074
|
-
Sometimes it's desirable to filter out from log files some
|
1089
|
+
Sometimes it's desirable to filter out from log files some sensitive locations your application is redirecting to.
|
1075
1090
|
You can do that by using the `config.filter_redirect` configuration option:
|
1076
1091
|
|
1077
1092
|
```ruby
|
@@ -1149,8 +1164,65 @@ class ClientsController < ApplicationController
|
|
1149
1164
|
end
|
1150
1165
|
```
|
1151
1166
|
|
1167
|
+
WARNING: You shouldn't do `rescue_from Exception` or `rescue_from StandardError` unless you have a particular reason as it will cause serious side-effects (e.g. you won't be able to see exception details and tracebacks during development). If you would like to dynamically generate error pages, see [Custom errors page](#custom-errors-page).
|
1168
|
+
|
1152
1169
|
NOTE: Certain exceptions are only rescuable from the `ApplicationController` class, as they are raised before the controller gets initialized and the action gets executed. See Pratik Naik's [article](http://m.onkey.org/2008/7/20/rescue-from-dispatching) on the subject for more information.
|
1153
1170
|
|
1171
|
+
|
1172
|
+
### Custom errors page
|
1173
|
+
|
1174
|
+
You can customize the layout of your error handling using controllers and views.
|
1175
|
+
First define your app own routes to display the errors page.
|
1176
|
+
|
1177
|
+
* `config/application.rb`
|
1178
|
+
|
1179
|
+
```ruby
|
1180
|
+
config.exceptions_app = self.routes
|
1181
|
+
```
|
1182
|
+
|
1183
|
+
* `config/routes.rb`
|
1184
|
+
|
1185
|
+
```ruby
|
1186
|
+
get '/404', to: 'errors#not_found'
|
1187
|
+
get '/422', to: 'errors#unprocessable_entity'
|
1188
|
+
get '/500', to: 'errors#server_error'
|
1189
|
+
```
|
1190
|
+
|
1191
|
+
Create the controller and views.
|
1192
|
+
|
1193
|
+
* `app/controllers/errors_controller.rb`
|
1194
|
+
|
1195
|
+
```ruby
|
1196
|
+
class ErrorsController < ActionController::Base
|
1197
|
+
layout 'error'
|
1198
|
+
|
1199
|
+
def not_found
|
1200
|
+
render status: :not_found
|
1201
|
+
end
|
1202
|
+
|
1203
|
+
def unprocessable_entity
|
1204
|
+
render status: :unprocessable_entity
|
1205
|
+
end
|
1206
|
+
|
1207
|
+
def server_error
|
1208
|
+
render status: :server_error
|
1209
|
+
end
|
1210
|
+
end
|
1211
|
+
```
|
1212
|
+
|
1213
|
+
* `app/views`
|
1214
|
+
|
1215
|
+
```
|
1216
|
+
errors/
|
1217
|
+
not_found.html.erb
|
1218
|
+
unprocessable_entity.html.erb
|
1219
|
+
server_error.html.erb
|
1220
|
+
layouts/
|
1221
|
+
error.html.erb
|
1222
|
+
```
|
1223
|
+
|
1224
|
+
Do not forget to set the correct status code on the controller as shown before. You should avoid using the database or any complex operations because the user is already on the error page. Generating another error while on an error page could cause issues.
|
1225
|
+
|
1154
1226
|
Force HTTPS protocol
|
1155
1227
|
--------------------
|
1156
1228
|
|
@@ -17,7 +17,10 @@ After reading this guide, you will know:
|
|
17
17
|
Introduction
|
18
18
|
------------
|
19
19
|
|
20
|
-
Action Mailer allows you to send emails from your application using mailer classes
|
20
|
+
Action Mailer allows you to send emails from your application using mailer classes
|
21
|
+
and views. Mailers work very similarly to controllers. They inherit from
|
22
|
+
`ActionMailer::Base` and live in `app/mailers`, and they have associated views
|
23
|
+
that appear in `app/views`.
|
21
24
|
|
22
25
|
Sending Emails
|
23
26
|
--------------
|
@@ -84,8 +87,11 @@ Here is a quick explanation of the items presented in the preceding method. For
|
|
84
87
|
a full list of all available options, please have a look further down at the
|
85
88
|
Complete List of Action Mailer user-settable attributes section.
|
86
89
|
|
87
|
-
* `default Hash` - This is a hash of default values for any email you send from
|
88
|
-
|
90
|
+
* `default Hash` - This is a hash of default values for any email you send from
|
91
|
+
this mailer. In this case we are setting the `:from` header to a value for all
|
92
|
+
messages in this class. This can be overridden on a per-email basis.
|
93
|
+
* `mail` - The actual email message, we are passing the `:to` and `:subject`
|
94
|
+
headers in.
|
89
95
|
|
90
96
|
Just like controllers, any instance variables we define in the method become
|
91
97
|
available for use in the views.
|
@@ -151,7 +157,7 @@ $ bin/rake db:migrate
|
|
151
157
|
```
|
152
158
|
|
153
159
|
Now that we have a user model to play with, we will just edit the
|
154
|
-
`app/controllers/users_controller.rb` make it instruct the UserMailer to deliver
|
160
|
+
`app/controllers/users_controller.rb` make it instruct the `UserMailer` to deliver
|
155
161
|
an email to the newly created user by editing the create action and inserting a
|
156
162
|
call to `UserMailer.welcome_email` right after the user is successfully saved:
|
157
163
|
|
@@ -230,9 +236,11 @@ different, encode your content and pass in the encoded content and encoding in a
|
|
230
236
|
|
231
237
|
```ruby
|
232
238
|
encoded_content = SpecialEncode(File.read('/path/to/filename.jpg'))
|
233
|
-
attachments['filename.jpg'] = {
|
234
|
-
|
235
|
-
|
239
|
+
attachments['filename.jpg'] = {
|
240
|
+
mime_type: 'application/x-gzip',
|
241
|
+
encoding: 'SpecialEncoding',
|
242
|
+
content: encoded_content
|
243
|
+
}
|
236
244
|
```
|
237
245
|
|
238
246
|
NOTE: If you specify an encoding, Mail will assume that your content is already
|
@@ -301,7 +309,7 @@ email address in the format `"Full Name <email>"`.
|
|
301
309
|
```ruby
|
302
310
|
def welcome_email(user)
|
303
311
|
@user = user
|
304
|
-
email_with_name = "#{@user.name} <#{@user.email}>
|
312
|
+
email_with_name = %("#{@user.name}" <#{@user.email}>)
|
305
313
|
mail(to: email_with_name, subject: 'Welcome to My Awesome Site')
|
306
314
|
end
|
307
315
|
```
|
@@ -406,6 +414,22 @@ globally in `config/application.rb`:
|
|
406
414
|
config.action_mailer.default_url_options = { host: 'example.com' }
|
407
415
|
```
|
408
416
|
|
417
|
+
Because of this behavior you cannot use any of the `*_path` helpers inside of
|
418
|
+
an email. Instead you will need to use the associated `*_url` helper. For example
|
419
|
+
instead of using
|
420
|
+
|
421
|
+
```
|
422
|
+
<%= link_to 'welcome', welcome_path %>
|
423
|
+
```
|
424
|
+
|
425
|
+
You will need to use:
|
426
|
+
|
427
|
+
```
|
428
|
+
<%= link_to 'welcome', welcome_url %>
|
429
|
+
```
|
430
|
+
|
431
|
+
By using the full URL, your links will now work in your emails.
|
432
|
+
|
409
433
|
#### generating URLs with `url_for`
|
410
434
|
|
411
435
|
You need to pass the `only_path: false` option when using `url_for`. This will
|
@@ -608,7 +632,7 @@ files (environment.rb, production.rb, etc...)
|
|
608
632
|
| Configuration | Description |
|
609
633
|
|---------------|-------------|
|
610
634
|
|`logger`|Generates information on the mailing run if available. Can be set to `nil` for no logging. Compatible with both Ruby's own `Logger` and `Log4r` loggers.|
|
611
|
-
|`smtp_settings`|Allows detailed configuration for `:smtp` delivery method:<ul><li>`:address` - Allows you to use a remote mail server. Just change it from its default "localhost" setting.</li><li>`:port` - On the off chance that your mail server doesn't run on port 25, you can change it.</li><li>`:domain` - If you need to specify a HELO domain, you can do it here.</li><li>`:user_name` - If your mail server requires authentication, set the username in this setting.</li><li>`:password` - If your mail server requires authentication, set the password in this setting.</li><li>`:authentication` - If your mail server requires authentication, you need to specify the authentication type here. This is a symbol and one of `:plain`, `:login`, `:cram_md5`.</li><li>`:enable_starttls_auto` - Set this to `false` if there is a problem with your server certificate that you cannot resolve.</li></ul>|
|
635
|
+
|`smtp_settings`|Allows detailed configuration for `:smtp` delivery method:<ul><li>`:address` - Allows you to use a remote mail server. Just change it from its default `"localhost"` setting.</li><li>`:port` - On the off chance that your mail server doesn't run on port 25, you can change it.</li><li>`:domain` - If you need to specify a HELO domain, you can do it here.</li><li>`:user_name` - If your mail server requires authentication, set the username in this setting.</li><li>`:password` - If your mail server requires authentication, set the password in this setting.</li><li>`:authentication` - If your mail server requires authentication, you need to specify the authentication type here. This is a symbol and one of `:plain`, `:login`, `:cram_md5`.</li><li>`:enable_starttls_auto` - Set this to `false` if there is a problem with your server certificate that you cannot resolve.</li></ul>|
|
612
636
|
|`sendmail_settings`|Allows you to override options for the `:sendmail` delivery method.<ul><li>`:location` - The location of the sendmail executable. Defaults to `/usr/sbin/sendmail`.</li><li>`:arguments` - The command line arguments to be passed to sendmail. Defaults to `-i -t`.</li></ul>|
|
613
637
|
|`raise_delivery_errors`|Whether or not errors should be raised if the email fails to be delivered. This only works if the external email server is configured for immediate delivery.|
|
614
638
|
|`delivery_method`|Defines a delivery method. Possible values are:<ul><li>`:smtp` (default), can be configured by using `config.action_mailer.smtp_settings`.</li><li>`:sendmail`, can be configured by using `config.action_mailer.sendmail_settings`.</li><li>`:file`: save emails to files; can be configured by using `config.action_mailer.file_settings`.</li><li>`:test`: save emails to `ActionMailer::Base.deliveries` array.</li></ul>See [API docs](http://api.rubyonrails.org/classes/ActionMailer/Base.html) for more info.|
|
@@ -617,7 +641,7 @@ files (environment.rb, production.rb, etc...)
|
|
617
641
|
|`default_options`|Allows you to set default values for the `mail` method options (`:from`, `:reply_to`, etc.).|
|
618
642
|
|
619
643
|
For a complete writeup of possible configurations see the
|
620
|
-
[Action Mailer
|
644
|
+
[Configuring Action Mailer](configuring.html#configuring-action-mailer) in
|
621
645
|
our Configuring Rails Applications guide.
|
622
646
|
|
623
647
|
### Example Action Mailer Configuration
|
@@ -662,6 +686,7 @@ You can find detailed instructions on how to test your mailers in the
|
|
662
686
|
|
663
687
|
Intercepting Emails
|
664
688
|
-------------------
|
689
|
+
|
665
690
|
There are situations where you need to edit an email before it's
|
666
691
|
delivered. Fortunately Action Mailer provides hooks to intercept every
|
667
692
|
email. You can register an interceptor to make modifications to mail messages
|
@@ -685,5 +710,5 @@ ActionMailer::Base.register_interceptor(SandboxEmailInterceptor) if Rails.env.st
|
|
685
710
|
|
686
711
|
NOTE: The example above uses a custom environment called "staging" for a
|
687
712
|
production like server but for testing purposes. You can read
|
688
|
-
[Creating Rails environments](
|
713
|
+
[Creating Rails environments](configuring.html#creating-rails-environments)
|
689
714
|
for more information about custom Rails environments.
|
@@ -28,22 +28,22 @@ For each controller there is an associated directory in the `app/views` director
|
|
28
28
|
Let's take a look at what Rails does by default when creating a new resource using the scaffold generator:
|
29
29
|
|
30
30
|
```bash
|
31
|
-
$ bin/rails generate scaffold
|
31
|
+
$ bin/rails generate scaffold article
|
32
32
|
[...]
|
33
33
|
invoke scaffold_controller
|
34
|
-
create app/controllers/
|
34
|
+
create app/controllers/articles_controller.rb
|
35
35
|
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/
|
36
|
+
create app/views/articles
|
37
|
+
create app/views/articles/index.html.erb
|
38
|
+
create app/views/articles/edit.html.erb
|
39
|
+
create app/views/articles/show.html.erb
|
40
|
+
create app/views/articles/new.html.erb
|
41
|
+
create app/views/articles/_form.html.erb
|
42
42
|
[...]
|
43
43
|
```
|
44
44
|
|
45
45
|
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 `
|
46
|
+
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.
|
47
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. Later on this guide you can find a more detailed documentation of each one of these three components.
|
48
48
|
|
49
49
|
|
@@ -276,23 +276,23 @@ Partial Layouts
|
|
276
276
|
|
277
277
|
Partials can have their own layouts applied to them. These layouts are different than the ones that are specified globally for the entire action, but they work in a similar fashion.
|
278
278
|
|
279
|
-
Let's say we're displaying
|
279
|
+
Let's say we're displaying an article on a page, that should be wrapped in a `div` for display purposes. First, we'll create a new `Article`:
|
280
280
|
|
281
281
|
```ruby
|
282
|
-
|
282
|
+
Article.create(body: 'Partial Layouts are cool!')
|
283
283
|
```
|
284
284
|
|
285
|
-
In the `show` template, we'll render the `
|
285
|
+
In the `show` template, we'll render the `_article` partial wrapped in the `box` layout:
|
286
286
|
|
287
|
-
**
|
287
|
+
**articles/show.html.erb**
|
288
288
|
|
289
289
|
```erb
|
290
|
-
<%= render partial: '
|
290
|
+
<%= render partial: 'article', layout: 'box', locals: {article: @article} %>
|
291
291
|
```
|
292
292
|
|
293
|
-
The `box` layout simply wraps the `
|
293
|
+
The `box` layout simply wraps the `_article` partial in a `div`:
|
294
294
|
|
295
|
-
**
|
295
|
+
**articles/_box.html.erb**
|
296
296
|
|
297
297
|
```html+erb
|
298
298
|
<div class='box'>
|
@@ -300,13 +300,13 @@ The `box` layout simply wraps the `_post` partial in a `div`:
|
|
300
300
|
</div>
|
301
301
|
```
|
302
302
|
|
303
|
-
The `
|
303
|
+
The `_article` partial wraps the article's `body` in a `div` with the `id` of the article using the `div_for` helper:
|
304
304
|
|
305
|
-
**
|
305
|
+
**articles/_article.html.erb**
|
306
306
|
|
307
307
|
```html+erb
|
308
|
-
<%= div_for(
|
309
|
-
<p><%=
|
308
|
+
<%= div_for(article) do %>
|
309
|
+
<p><%= article.body %></p>
|
310
310
|
<% end %>
|
311
311
|
```
|
312
312
|
|
@@ -314,22 +314,22 @@ this would output the following:
|
|
314
314
|
|
315
315
|
```html
|
316
316
|
<div class='box'>
|
317
|
-
<div id='
|
317
|
+
<div id='article_1'>
|
318
318
|
<p>Partial Layouts are cool!</p>
|
319
319
|
</div>
|
320
320
|
</div>
|
321
321
|
```
|
322
322
|
|
323
|
-
Note that the partial layout has access to the local `
|
323
|
+
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
324
|
|
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 `
|
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 `_article` partial, we could do this instead:
|
326
326
|
|
327
|
-
**
|
327
|
+
**articles/show.html.erb**
|
328
328
|
|
329
329
|
```html+erb
|
330
|
-
<% render(layout: 'box', locals: {
|
331
|
-
<%= div_for(
|
332
|
-
<p><%=
|
330
|
+
<% render(layout: 'box', locals: {article: @article}) do %>
|
331
|
+
<%= div_for(article) do %>
|
332
|
+
<p><%= article.body %></p>
|
333
333
|
<% end %>
|
334
334
|
<% end %>
|
335
335
|
```
|
@@ -356,18 +356,18 @@ This module provides methods for generating container tags, such as `div`, for y
|
|
356
356
|
|
357
357
|
Renders a container tag that relates to your Active Record Object.
|
358
358
|
|
359
|
-
For example, given `@
|
359
|
+
For example, given `@article` is the object of `Article` class, you can do:
|
360
360
|
|
361
361
|
```html+erb
|
362
|
-
<%= content_tag_for(:tr, @
|
363
|
-
<td><%= @
|
362
|
+
<%= content_tag_for(:tr, @article) do %>
|
363
|
+
<td><%= @article.title %></td>
|
364
364
|
<% end %>
|
365
365
|
```
|
366
366
|
|
367
367
|
This will generate this HTML output:
|
368
368
|
|
369
369
|
```html
|
370
|
-
<tr id="
|
370
|
+
<tr id="article_1234" class="article">
|
371
371
|
<td>Hello World!</td>
|
372
372
|
</tr>
|
373
373
|
```
|
@@ -375,34 +375,34 @@ This will generate this HTML output:
|
|
375
375
|
You can also supply HTML attributes as an additional option hash. For example:
|
376
376
|
|
377
377
|
```html+erb
|
378
|
-
<%= content_tag_for(:tr, @
|
379
|
-
<td><%= @
|
378
|
+
<%= content_tag_for(:tr, @article, class: "frontpage") do %>
|
379
|
+
<td><%= @article.title %></td>
|
380
380
|
<% end %>
|
381
381
|
```
|
382
382
|
|
383
383
|
Will generate this HTML output:
|
384
384
|
|
385
385
|
```html
|
386
|
-
<tr id="
|
386
|
+
<tr id="article_1234" class="article frontpage">
|
387
387
|
<td>Hello World!</td>
|
388
388
|
</tr>
|
389
389
|
```
|
390
390
|
|
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 `@
|
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 `@articles` is an array of two `Article` objects:
|
392
392
|
|
393
393
|
```html+erb
|
394
|
-
<%= content_tag_for(:tr, @
|
395
|
-
<td><%=
|
394
|
+
<%= content_tag_for(:tr, @articles) do |article| %>
|
395
|
+
<td><%= article.title %></td>
|
396
396
|
<% end %>
|
397
397
|
```
|
398
398
|
|
399
399
|
Will generate this HTML output:
|
400
400
|
|
401
401
|
```html
|
402
|
-
<tr id="
|
402
|
+
<tr id="article_1234" class="article">
|
403
403
|
<td>Hello World!</td>
|
404
404
|
</tr>
|
405
|
-
<tr id="
|
405
|
+
<tr id="article_1235" class="article">
|
406
406
|
<td>Ruby on Rails Rocks!</td>
|
407
407
|
</tr>
|
408
408
|
```
|
@@ -412,15 +412,15 @@ Will generate this HTML output:
|
|
412
412
|
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
413
|
|
414
414
|
```html+erb
|
415
|
-
<%= div_for(@
|
416
|
-
<td><%= @
|
415
|
+
<%= div_for(@article, class: "frontpage") do %>
|
416
|
+
<td><%= @article.title %></td>
|
417
417
|
<% end %>
|
418
418
|
```
|
419
419
|
|
420
420
|
Will generate this HTML output:
|
421
421
|
|
422
422
|
```html
|
423
|
-
<div id="
|
423
|
+
<div id="article_1234" class="article frontpage">
|
424
424
|
<td>Hello World!</td>
|
425
425
|
</div>
|
426
426
|
```
|
@@ -495,7 +495,7 @@ image_url("edit.png") # => http://www.example.com/assets/edit.png
|
|
495
495
|
|
496
496
|
#### image_tag
|
497
497
|
|
498
|
-
Returns an
|
498
|
+
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
499
|
|
500
500
|
```ruby
|
501
501
|
image_tag("icon.png") # => <img src="/assets/icon.png" alt="Icon" />
|
@@ -503,7 +503,7 @@ image_tag("icon.png") # => <img src="/assets/icon.png" alt="Icon" />
|
|
503
503
|
|
504
504
|
#### javascript_include_tag
|
505
505
|
|
506
|
-
Returns an
|
506
|
+
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
507
|
|
508
508
|
```ruby
|
509
509
|
javascript_include_tag "common" # => <script src="/assets/common.js"></script>
|
@@ -590,14 +590,14 @@ This helper makes building an Atom feed easy. Here's a full usage example:
|
|
590
590
|
**config/routes.rb**
|
591
591
|
|
592
592
|
```ruby
|
593
|
-
resources :
|
593
|
+
resources :articles
|
594
594
|
```
|
595
595
|
|
596
|
-
**app/controllers/
|
596
|
+
**app/controllers/articles_controller.rb**
|
597
597
|
|
598
598
|
```ruby
|
599
599
|
def index
|
600
|
-
@
|
600
|
+
@articles = Article.all
|
601
601
|
|
602
602
|
respond_to do |format|
|
603
603
|
format.html
|
@@ -606,20 +606,20 @@ def index
|
|
606
606
|
end
|
607
607
|
```
|
608
608
|
|
609
|
-
**app/views/
|
609
|
+
**app/views/articles/index.atom.builder**
|
610
610
|
|
611
611
|
```ruby
|
612
612
|
atom_feed do |feed|
|
613
|
-
feed.title("
|
614
|
-
feed.updated((@
|
613
|
+
feed.title("Articles Index")
|
614
|
+
feed.updated((@articles.first.created_at))
|
615
615
|
|
616
|
-
@
|
617
|
-
feed.entry(
|
618
|
-
entry.title(
|
619
|
-
entry.content(
|
616
|
+
@articles.each do |article|
|
617
|
+
feed.entry(article) do |entry|
|
618
|
+
entry.title(article.title)
|
619
|
+
entry.content(article.body, type: 'html')
|
620
620
|
|
621
621
|
entry.author do |author|
|
622
|
-
author.name(
|
622
|
+
author.name(article.author_name)
|
623
623
|
end
|
624
624
|
end
|
625
625
|
end
|
@@ -697,7 +697,7 @@ For example, let's say we have a standard application layout, but also a special
|
|
697
697
|
</html>
|
698
698
|
```
|
699
699
|
|
700
|
-
**app/views/
|
700
|
+
**app/views/articles/special.html.erb**
|
701
701
|
|
702
702
|
```html+erb
|
703
703
|
<p>This is a special page.</p>
|
@@ -714,7 +714,7 @@ For example, let's say we have a standard application layout, but also a special
|
|
714
714
|
Returns a set of select tags (one for year, month, and day) pre-selected for accessing a specified date-based attribute.
|
715
715
|
|
716
716
|
```ruby
|
717
|
-
date_select("
|
717
|
+
date_select("article", "published_on")
|
718
718
|
```
|
719
719
|
|
720
720
|
#### datetime_select
|
@@ -722,7 +722,7 @@ date_select("post", "published_on")
|
|
722
722
|
Returns a set of select tags (one for year, month, day, hour, and minute) pre-selected for accessing a specified datetime-based attribute.
|
723
723
|
|
724
724
|
```ruby
|
725
|
-
datetime_select("
|
725
|
+
datetime_select("article", "published_on")
|
726
726
|
```
|
727
727
|
|
728
728
|
#### distance_of_time_in_words
|
@@ -736,7 +736,7 @@ distance_of_time_in_words(Time.now, Time.now + 15.seconds, include_seconds: true
|
|
736
736
|
|
737
737
|
#### select_date
|
738
738
|
|
739
|
-
Returns a set of
|
739
|
+
Returns a set of HTML select-tags (one for year, month, and day) pre-selected with the `date` provided.
|
740
740
|
|
741
741
|
```ruby
|
742
742
|
# Generates a date select that defaults to the date provided (six days after today)
|
@@ -748,7 +748,7 @@ select_date()
|
|
748
748
|
|
749
749
|
#### select_datetime
|
750
750
|
|
751
|
-
Returns a set of
|
751
|
+
Returns a set of HTML select-tags (one for year, month, day, hour, and minute) pre-selected with the `datetime` provided.
|
752
752
|
|
753
753
|
```ruby
|
754
754
|
# Generates a datetime select that defaults to the datetime provided (four days after today)
|
@@ -808,7 +808,7 @@ select_second(Time.now + 16.minutes)
|
|
808
808
|
|
809
809
|
#### select_time
|
810
810
|
|
811
|
-
Returns a set of
|
811
|
+
Returns a set of HTML select-tags (one for hour and minute).
|
812
812
|
|
813
813
|
```ruby
|
814
814
|
# Generates a time select that defaults to the time provided
|
@@ -904,10 +904,10 @@ The params hash has a nested person value, which can therefore be accessed with
|
|
904
904
|
Returns a checkbox tag tailored for accessing a specified attribute.
|
905
905
|
|
906
906
|
```ruby
|
907
|
-
# Let's say that @
|
908
|
-
check_box("
|
909
|
-
# => <input type="checkbox" id="
|
910
|
-
# <input name="
|
907
|
+
# Let's say that @article.validated? is 1:
|
908
|
+
check_box("article", "validated")
|
909
|
+
# => <input type="checkbox" id="article_validated" name="article[validated]" value="1" />
|
910
|
+
# <input name="article[validated]" type="hidden" value="0" />
|
911
911
|
```
|
912
912
|
|
913
913
|
#### fields_for
|
@@ -939,7 +939,7 @@ file_field(:user, :avatar)
|
|
939
939
|
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
940
|
|
941
941
|
```html+erb
|
942
|
-
<%= form_for @
|
942
|
+
<%= form_for @article do |f| %>
|
943
943
|
<%= f.label :title, 'Title' %>:
|
944
944
|
<%= f.text_field :title %><br>
|
945
945
|
<%= f.label :body, 'Body' %>:
|
@@ -961,8 +961,8 @@ hidden_field(:user, :token)
|
|
961
961
|
Returns a label tag tailored for labelling an input field for a specified attribute.
|
962
962
|
|
963
963
|
```ruby
|
964
|
-
label(:
|
965
|
-
# => <label for="
|
964
|
+
label(:article, :title)
|
965
|
+
# => <label for="article_title">Title</label>
|
966
966
|
```
|
967
967
|
|
968
968
|
#### password_field
|
@@ -979,11 +979,11 @@ password_field(:login, :pass)
|
|
979
979
|
Returns a radio button tag for accessing a specified attribute.
|
980
980
|
|
981
981
|
```ruby
|
982
|
-
# Let's say that @
|
983
|
-
radio_button("
|
984
|
-
radio_button("
|
985
|
-
# => <input type="radio" id="
|
986
|
-
# <input type="radio" id="
|
982
|
+
# Let's say that @article.category returns "rails":
|
983
|
+
radio_button("article", "category", "rails")
|
984
|
+
radio_button("article", "category", "java")
|
985
|
+
# => <input type="radio" id="article_category_rails" name="article[category]" value="rails" checked="checked" />
|
986
|
+
# <input type="radio" id="article_category_java" name="article[category]" value="java" />
|
987
987
|
```
|
988
988
|
|
989
989
|
#### text_area
|
@@ -1002,8 +1002,8 @@ text_area(:comment, :text, size: "20x30")
|
|
1002
1002
|
Returns an input tag of the "text" type tailored for accessing a specified attribute.
|
1003
1003
|
|
1004
1004
|
```ruby
|
1005
|
-
text_field(:
|
1006
|
-
# => <input type="text" id="
|
1005
|
+
text_field(:article, :title)
|
1006
|
+
# => <input type="text" id="article_title" name="article[title]" value="#{@article.title}" />
|
1007
1007
|
```
|
1008
1008
|
|
1009
1009
|
#### email_field
|
@@ -1035,28 +1035,28 @@ Returns `select` and `option` tags for the collection of existing return values
|
|
1035
1035
|
Example object structure for use with this method:
|
1036
1036
|
|
1037
1037
|
```ruby
|
1038
|
-
class
|
1038
|
+
class Article < ActiveRecord::Base
|
1039
1039
|
belongs_to :author
|
1040
1040
|
end
|
1041
1041
|
|
1042
1042
|
class Author < ActiveRecord::Base
|
1043
|
-
has_many :
|
1043
|
+
has_many :articles
|
1044
1044
|
def name_with_initial
|
1045
1045
|
"#{first_name.first}. #{last_name}"
|
1046
1046
|
end
|
1047
1047
|
end
|
1048
1048
|
```
|
1049
1049
|
|
1050
|
-
Sample usage (selecting the associated Author for an instance of
|
1050
|
+
Sample usage (selecting the associated Author for an instance of Article, `@article`):
|
1051
1051
|
|
1052
1052
|
```ruby
|
1053
|
-
collection_select(:
|
1053
|
+
collection_select(:article, :author_id, Author.all, :id, :name_with_initial, {prompt: true})
|
1054
1054
|
```
|
1055
1055
|
|
1056
|
-
If `@
|
1056
|
+
If `@article.author_id` is 1, this would return:
|
1057
1057
|
|
1058
1058
|
```html
|
1059
|
-
<select name="
|
1059
|
+
<select name="article[author_id]">
|
1060
1060
|
<option value="">Please select</option>
|
1061
1061
|
<option value="1" selected="selected">D. Heinemeier Hansson</option>
|
1062
1062
|
<option value="2">D. Thomas</option>
|
@@ -1071,33 +1071,33 @@ Returns `radio_button` tags for the collection of existing return values of `met
|
|
1071
1071
|
Example object structure for use with this method:
|
1072
1072
|
|
1073
1073
|
```ruby
|
1074
|
-
class
|
1074
|
+
class Article < ActiveRecord::Base
|
1075
1075
|
belongs_to :author
|
1076
1076
|
end
|
1077
1077
|
|
1078
1078
|
class Author < ActiveRecord::Base
|
1079
|
-
has_many :
|
1079
|
+
has_many :articles
|
1080
1080
|
def name_with_initial
|
1081
1081
|
"#{first_name.first}. #{last_name}"
|
1082
1082
|
end
|
1083
1083
|
end
|
1084
1084
|
```
|
1085
1085
|
|
1086
|
-
Sample usage (selecting the associated Author for an instance of
|
1086
|
+
Sample usage (selecting the associated Author for an instance of Article, `@article`):
|
1087
1087
|
|
1088
1088
|
```ruby
|
1089
|
-
collection_radio_buttons(:
|
1089
|
+
collection_radio_buttons(:article, :author_id, Author.all, :id, :name_with_initial)
|
1090
1090
|
```
|
1091
1091
|
|
1092
|
-
If `@
|
1092
|
+
If `@article.author_id` is 1, this would return:
|
1093
1093
|
|
1094
1094
|
```html
|
1095
|
-
<input id="
|
1096
|
-
<label for="
|
1097
|
-
<input id="
|
1098
|
-
<label for="
|
1099
|
-
<input id="
|
1100
|
-
<label for="
|
1095
|
+
<input id="article_author_id_1" name="article[author_id]" type="radio" value="1" checked="checked" />
|
1096
|
+
<label for="article_author_id_1">D. Heinemeier Hansson</label>
|
1097
|
+
<input id="article_author_id_2" name="article[author_id]" type="radio" value="2" />
|
1098
|
+
<label for="article_author_id_2">D. Thomas</label>
|
1099
|
+
<input id="article_author_id_3" name="article[author_id]" type="radio" value="3" />
|
1100
|
+
<label for="article_author_id_3">M. Clark</label>
|
1101
1101
|
```
|
1102
1102
|
|
1103
1103
|
#### collection_check_boxes
|
@@ -1107,36 +1107,44 @@ Returns `check_box` tags for the collection of existing return values of `method
|
|
1107
1107
|
Example object structure for use with this method:
|
1108
1108
|
|
1109
1109
|
```ruby
|
1110
|
-
class
|
1110
|
+
class Article < ActiveRecord::Base
|
1111
1111
|
has_and_belongs_to_many :authors
|
1112
1112
|
end
|
1113
1113
|
|
1114
1114
|
class Author < ActiveRecord::Base
|
1115
|
-
has_and_belongs_to_many :
|
1115
|
+
has_and_belongs_to_many :articles
|
1116
1116
|
def name_with_initial
|
1117
1117
|
"#{first_name.first}. #{last_name}"
|
1118
1118
|
end
|
1119
1119
|
end
|
1120
1120
|
```
|
1121
1121
|
|
1122
|
-
Sample usage (selecting the associated Authors for an instance of
|
1122
|
+
Sample usage (selecting the associated Authors for an instance of Article, `@article`):
|
1123
1123
|
|
1124
1124
|
```ruby
|
1125
|
-
collection_check_boxes(:
|
1125
|
+
collection_check_boxes(:article, :author_ids, Author.all, :id, :name_with_initial)
|
1126
1126
|
```
|
1127
1127
|
|
1128
|
-
If `@
|
1128
|
+
If `@article.author_ids` is [1], this would return:
|
1129
1129
|
|
1130
1130
|
```html
|
1131
|
-
<input id="
|
1132
|
-
<label for="
|
1133
|
-
<input id="
|
1134
|
-
<label for="
|
1135
|
-
<input id="
|
1136
|
-
<label for="
|
1137
|
-
<input name="
|
1131
|
+
<input id="article_author_ids_1" name="article[author_ids][]" type="checkbox" value="1" checked="checked" />
|
1132
|
+
<label for="article_author_ids_1">D. Heinemeier Hansson</label>
|
1133
|
+
<input id="article_author_ids_2" name="article[author_ids][]" type="checkbox" value="2" />
|
1134
|
+
<label for="article_author_ids_2">D. Thomas</label>
|
1135
|
+
<input id="article_author_ids_3" name="article[author_ids][]" type="checkbox" value="3" />
|
1136
|
+
<label for="article_author_ids_3">M. Clark</label>
|
1137
|
+
<input name="article[author_ids][]" type="hidden" value="" />
|
1138
1138
|
```
|
1139
1139
|
|
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
|
+
|
1140
1148
|
#### option_groups_from_collection_for_select
|
1141
1149
|
|
1142
1150
|
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.
|
@@ -1214,13 +1222,13 @@ Create a select tag and a series of contained option tags for the provided objec
|
|
1214
1222
|
Example:
|
1215
1223
|
|
1216
1224
|
```ruby
|
1217
|
-
select("
|
1225
|
+
select("article", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, {include_blank: true})
|
1218
1226
|
```
|
1219
1227
|
|
1220
|
-
If `@
|
1228
|
+
If `@article.person_id` is 1, this would become:
|
1221
1229
|
|
1222
1230
|
```html
|
1223
|
-
<select name="
|
1231
|
+
<select name="article[person_id]">
|
1224
1232
|
<option value=""></option>
|
1225
1233
|
<option value="1" selected="selected">David</option>
|
1226
1234
|
<option value="2">Sam</option>
|
@@ -1295,10 +1303,10 @@ file_field_tag 'attachment'
|
|
1295
1303
|
Starts a form tag that points the action to an url configured with `url_for_options` just like `ActionController::Base#url_for`.
|
1296
1304
|
|
1297
1305
|
```html+erb
|
1298
|
-
<%= form_tag '/
|
1306
|
+
<%= form_tag '/articles' do %>
|
1299
1307
|
<div><%= submit_tag 'Save' %></div>
|
1300
1308
|
<% end %>
|
1301
|
-
# => <form action="/
|
1309
|
+
# => <form action="/articles" method="post"><div><input type="submit" name="submit" value="Save" /></div></form>
|
1302
1310
|
```
|
1303
1311
|
|
1304
1312
|
#### hidden_field_tag
|
@@ -1360,8 +1368,8 @@ select_tag "people", "<option>David</option>"
|
|
1360
1368
|
Creates a submit button with the text provided as the caption.
|
1361
1369
|
|
1362
1370
|
```ruby
|
1363
|
-
submit_tag "Publish this
|
1364
|
-
# => <input name="commit" type="submit" value="Publish this
|
1371
|
+
submit_tag "Publish this article"
|
1372
|
+
# => <input name="commit" type="submit" value="Publish this article" />
|
1365
1373
|
```
|
1366
1374
|
|
1367
1375
|
#### text_area_tag
|
@@ -1369,8 +1377,8 @@ submit_tag "Publish this post"
|
|
1369
1377
|
Creates a text input area; use a textarea for longer text inputs such as blog posts or descriptions.
|
1370
1378
|
|
1371
1379
|
```ruby
|
1372
|
-
text_area_tag '
|
1373
|
-
# => <textarea id="
|
1380
|
+
text_area_tag 'article'
|
1381
|
+
# => <textarea id="article" name="article"></textarea>
|
1374
1382
|
```
|
1375
1383
|
|
1376
1384
|
#### text_field_tag
|
@@ -1518,7 +1526,7 @@ The SanitizeHelper module provides a set of methods for scrubbing text of undesi
|
|
1518
1526
|
|
1519
1527
|
#### sanitize
|
1520
1528
|
|
1521
|
-
This sanitize helper will
|
1529
|
+
This sanitize helper will HTML encode all tags and strip all attributes that aren't specifically allowed.
|
1522
1530
|
|
1523
1531
|
```ruby
|
1524
1532
|
sanitize @article.body
|
@@ -1542,7 +1550,7 @@ end
|
|
1542
1550
|
|
1543
1551
|
Sanitizes a block of CSS code.
|
1544
1552
|
|
1545
|
-
#### strip_links(html)
|
1553
|
+
#### strip_links(html)
|
1546
1554
|
Strips all link tags from text leaving just the link text.
|
1547
1555
|
|
1548
1556
|
```ruby
|
@@ -1560,9 +1568,9 @@ strip_links('Blog: <a href="http://myblog.com/">Visit</a>.')
|
|
1560
1568
|
# => Blog: Visit.
|
1561
1569
|
```
|
1562
1570
|
|
1563
|
-
#### strip_tags(html)
|
1571
|
+
#### strip_tags(html)
|
1564
1572
|
|
1565
|
-
Strips all HTML tags from the html, including comments.
|
1573
|
+
Strips all HTML tags from the html, including comments.
|
1566
1574
|
This uses the html-scanner tokenizer and so its HTML parsing ability is limited by that of html-scanner.
|
1567
1575
|
|
1568
1576
|
```ruby
|
@@ -1577,13 +1585,24 @@ strip_tags("<b>Bold</b> no more! <a href='more.html'>See more</a>")
|
|
1577
1585
|
|
1578
1586
|
NB: The output may still contain unescaped '<', '>', '&' characters and confuse browsers.
|
1579
1587
|
|
1588
|
+
### CsrfHelper
|
1589
|
+
|
1590
|
+
Returns meta tags "csrf-param" and "csrf-token" with the name of the cross-site
|
1591
|
+
request forgery protection parameter and token, respectively.
|
1592
|
+
|
1593
|
+
```html
|
1594
|
+
<%= csrf_meta_tags %>
|
1595
|
+
```
|
1596
|
+
|
1597
|
+
NOTE: Regular forms generate hidden fields so they do not use these tags. More
|
1598
|
+
details can be found in the [Rails Security Guide](security.html#cross-site-request-forgery-csrf).
|
1580
1599
|
|
1581
1600
|
Localized Views
|
1582
1601
|
---------------
|
1583
1602
|
|
1584
1603
|
Action View has the ability render different templates depending on the current locale.
|
1585
1604
|
|
1586
|
-
For example, suppose you have a `
|
1605
|
+
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.
|
1587
1606
|
|
1588
1607
|
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.
|
1589
1608
|
|
@@ -1597,6 +1616,6 @@ def set_expert_locale
|
|
1597
1616
|
end
|
1598
1617
|
```
|
1599
1618
|
|
1600
|
-
Then you could create special views like `app/views/
|
1619
|
+
Then you could create special views like `app/views/articles/show.expert.html.erb` that would only be displayed to expert users.
|
1601
1620
|
|
1602
1621
|
You can read more about the Rails Internationalization (I18n) API [here](i18n.html).
|