rails 4.1.16 → 4.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +12 -10
- data/guides/CHANGELOG.md +15 -100
- data/guides/Rakefile +5 -3
- data/guides/assets/javascripts/guides.js +6 -0
- data/guides/assets/stylesheets/main.css +4 -1
- data/guides/bug_report_templates/action_controller_master.rb +1 -0
- data/guides/rails_guides/helpers.rb +1 -1
- data/guides/rails_guides/levenshtein.rb +29 -21
- data/guides/rails_guides/markdown/renderer.rb +1 -1
- data/guides/rails_guides/markdown.rb +11 -7
- data/guides/rails_guides.rb +2 -2
- data/guides/source/2_2_release_notes.md +1 -1
- data/guides/source/2_3_release_notes.md +4 -4
- data/guides/source/3_0_release_notes.md +8 -8
- data/guides/source/3_1_release_notes.md +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 +5 -6
- data/guides/source/4_2_release_notes.md +850 -0
- data/guides/source/_license.html.erb +1 -1
- data/guides/source/_welcome.html.erb +2 -8
- data/guides/source/action_controller_overview.md +81 -7
- data/guides/source/action_mailer_basics.md +91 -28
- data/guides/source/action_view_overview.md +148 -130
- data/guides/source/active_job_basics.md +318 -0
- data/guides/source/active_model_basics.md +371 -17
- data/guides/source/active_record_basics.md +19 -18
- data/guides/source/active_record_callbacks.md +12 -9
- data/guides/source/{migrations.md → active_record_migrations.md} +95 -220
- data/guides/source/active_record_postgresql.md +433 -0
- data/guides/source/active_record_querying.md +263 -265
- data/guides/source/active_record_validations.md +20 -11
- data/guides/source/active_support_core_extensions.md +159 -72
- data/guides/source/active_support_instrumentation.md +10 -7
- data/guides/source/api_documentation_guidelines.md +62 -16
- data/guides/source/asset_pipeline.md +258 -63
- data/guides/source/association_basics.md +81 -74
- data/guides/source/caching_with_rails.md +32 -7
- data/guides/source/command_line.md +52 -30
- data/guides/source/configuring.md +132 -29
- data/guides/source/constant_autoloading_and_reloading.md +1297 -0
- data/guides/source/contributing_to_ruby_on_rails.md +192 -112
- data/guides/source/credits.html.erb +2 -2
- data/guides/source/debugging_rails_applications.md +440 -286
- data/guides/source/development_dependencies_install.md +47 -36
- data/guides/source/documents.yaml +19 -7
- data/guides/source/engines.md +182 -182
- data/guides/source/form_helpers.md +79 -56
- data/guides/source/generators.md +24 -11
- data/guides/source/getting_started.md +337 -198
- data/guides/source/i18n.md +108 -65
- data/guides/source/index.html.erb +1 -0
- data/guides/source/initialization.md +108 -61
- data/guides/source/layout.html.erb +1 -4
- data/guides/source/layouts_and_rendering.md +27 -25
- data/guides/source/maintenance_policy.md +6 -3
- 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 +8 -4
- data/guides/source/routing.md +98 -72
- data/guides/source/ruby_on_rails_guides_guidelines.md +11 -12
- data/guides/source/security.md +38 -32
- data/guides/source/testing.md +188 -117
- data/guides/source/upgrading_ruby_on_rails.md +254 -28
- data/guides/source/working_with_javascript_in_rails.md +18 -16
- data/guides/w3c_validator.rb +2 -0
- metadata +40 -96
- 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/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
@@ -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.8/">Rails 4.1.8</a>, <a href="http://guides.rubyonrails.org/v4.0.12/">Rails 4.0.12</a>, <a href="http://guides.rubyonrails.org/v3.2.21/">Rails 3.2.21</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,67 @@ 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
|
+
match '/404', via: :all, to: 'errors#not_found'
|
1187
|
+
match '/422', via: :all, to: 'errors#unprocessable_entity'
|
1188
|
+
match '/500', via: :all, 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.
|
1225
|
+
|
1226
|
+
WARNING: 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 like presenting an empty page for the users.
|
1227
|
+
|
1154
1228
|
Force HTTPS protocol
|
1155
1229
|
--------------------
|
1156
1230
|
|
@@ -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
|
--------------
|
@@ -32,10 +35,26 @@ views.
|
|
32
35
|
```bash
|
33
36
|
$ bin/rails generate mailer UserMailer
|
34
37
|
create app/mailers/user_mailer.rb
|
38
|
+
create app/mailers/application_mailer.rb
|
35
39
|
invoke erb
|
36
40
|
create app/views/user_mailer
|
41
|
+
create app/views/layouts/mailer.text.erb
|
42
|
+
create app/views/layouts/mailer.html.erb
|
37
43
|
invoke test_unit
|
38
44
|
create test/mailers/user_mailer_test.rb
|
45
|
+
create test/mailers/previews/user_mailer_preview.rb
|
46
|
+
```
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
# app/mailers/application_mailer.rb
|
50
|
+
class ApplicationMailer < ActionMailer::Base
|
51
|
+
default "from@example.com"
|
52
|
+
layout 'mailer'
|
53
|
+
end
|
54
|
+
|
55
|
+
# app/mailers/user_mailer.rb
|
56
|
+
class UserMailer < ApplicationMailer
|
57
|
+
end
|
39
58
|
```
|
40
59
|
|
41
60
|
As you can see, you can generate mailers just like you use other generators with
|
@@ -60,8 +79,7 @@ delivered via email.
|
|
60
79
|
`app/mailers/user_mailer.rb` contains an empty mailer:
|
61
80
|
|
62
81
|
```ruby
|
63
|
-
class UserMailer <
|
64
|
-
default from: 'from@example.com'
|
82
|
+
class UserMailer < ApplicationMailer
|
65
83
|
end
|
66
84
|
```
|
67
85
|
|
@@ -69,7 +87,7 @@ Let's add a method called `welcome_email`, that will send an email to the user's
|
|
69
87
|
registered email address:
|
70
88
|
|
71
89
|
```ruby
|
72
|
-
class UserMailer <
|
90
|
+
class UserMailer < ApplicationMailer
|
73
91
|
default from: 'notifications@example.com'
|
74
92
|
|
75
93
|
def welcome_email(user)
|
@@ -84,8 +102,11 @@ Here is a quick explanation of the items presented in the preceding method. For
|
|
84
102
|
a full list of all available options, please have a look further down at the
|
85
103
|
Complete List of Action Mailer user-settable attributes section.
|
86
104
|
|
87
|
-
* `default Hash` - This is a hash of default values for any email you send from
|
88
|
-
|
105
|
+
* `default Hash` - This is a hash of default values for any email you send from
|
106
|
+
this mailer. In this case we are setting the `:from` header to a value for all
|
107
|
+
messages in this class. This can be overridden on a per-email basis.
|
108
|
+
* `mail` - The actual email message, we are passing the `:to` and `:subject`
|
109
|
+
headers in.
|
89
110
|
|
90
111
|
Just like controllers, any instance variables we define in the method become
|
91
112
|
available for use in the views.
|
@@ -151,9 +172,12 @@ $ bin/rake db:migrate
|
|
151
172
|
```
|
152
173
|
|
153
174
|
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
|
175
|
+
`app/controllers/users_controller.rb` make it instruct the `UserMailer` to deliver
|
155
176
|
an email to the newly created user by editing the create action and inserting a
|
156
|
-
call to `UserMailer.welcome_email` right after the user is successfully saved
|
177
|
+
call to `UserMailer.welcome_email` right after the user is successfully saved.
|
178
|
+
|
179
|
+
Action Mailer is nicely integrated with Active Job so you can send emails outside
|
180
|
+
of the request-response cycle, so the user doesn't have to wait on it:
|
157
181
|
|
158
182
|
```ruby
|
159
183
|
class UsersController < ApplicationController
|
@@ -165,7 +189,7 @@ class UsersController < ApplicationController
|
|
165
189
|
respond_to do |format|
|
166
190
|
if @user.save
|
167
191
|
# Tell the UserMailer to send a welcome email after save
|
168
|
-
UserMailer.welcome_email(@user).
|
192
|
+
UserMailer.welcome_email(@user).deliver_later
|
169
193
|
|
170
194
|
format.html { redirect_to(@user, notice: 'User was successfully created.') }
|
171
195
|
format.json { render json: @user, status: :created, location: @user }
|
@@ -178,8 +202,29 @@ class UsersController < ApplicationController
|
|
178
202
|
end
|
179
203
|
```
|
180
204
|
|
181
|
-
|
182
|
-
|
205
|
+
NOTE: Active Job's default behavior is to execute jobs ':inline'. So, you can use
|
206
|
+
`deliver_later` now to send emails, and when you later decide to start sending
|
207
|
+
them from a background job, you'll only need to set up Active Job to use a queueing
|
208
|
+
backend (Sidekiq, Resque, etc).
|
209
|
+
|
210
|
+
If you want to send emails right away (from a cronjob for example) just call
|
211
|
+
`deliver_now`:
|
212
|
+
|
213
|
+
```ruby
|
214
|
+
class SendWeeklySummary
|
215
|
+
def run
|
216
|
+
User.find_each do |user|
|
217
|
+
UserMailer.weekly_summary(user).deliver_now
|
218
|
+
end
|
219
|
+
end
|
220
|
+
end
|
221
|
+
```
|
222
|
+
|
223
|
+
The method `welcome_email` returns a `ActionMailer::MessageDelivery` object which
|
224
|
+
can then just be told `deliver_now` or `deliver_later` to send itself out. The
|
225
|
+
`ActionMailer::MessageDelivery` object is just a wrapper around a `Mail::Message`. If
|
226
|
+
you want to inspect, alter or do anything else with the `Mail::Message` object you can
|
227
|
+
access it with the `message` method on the `ActionMailer::MessageDelivery` object.
|
183
228
|
|
184
229
|
### Auto encoding header values
|
185
230
|
|
@@ -230,9 +275,11 @@ different, encode your content and pass in the encoded content and encoding in a
|
|
230
275
|
|
231
276
|
```ruby
|
232
277
|
encoded_content = SpecialEncode(File.read('/path/to/filename.jpg'))
|
233
|
-
attachments['filename.jpg'] = {
|
234
|
-
|
235
|
-
|
278
|
+
attachments['filename.jpg'] = {
|
279
|
+
mime_type: 'application/x-gzip',
|
280
|
+
encoding: 'SpecialEncoding',
|
281
|
+
content: encoded_content
|
282
|
+
}
|
236
283
|
```
|
237
284
|
|
238
285
|
NOTE: If you specify an encoding, Mail will assume that your content is already
|
@@ -266,8 +313,7 @@ Action Mailer 3.0 makes inline attachments, which involved a lot of hacking in p
|
|
266
313
|
```html+erb
|
267
314
|
<p>Hello there, this is our image</p>
|
268
315
|
|
269
|
-
<%= image_tag attachments['image.jpg'].url, alt: 'My Photo',
|
270
|
-
class: 'photos' %>
|
316
|
+
<%= image_tag attachments['image.jpg'].url, alt: 'My Photo', class: 'photos' %>
|
271
317
|
```
|
272
318
|
|
273
319
|
#### Sending Email To Multiple Recipients
|
@@ -301,7 +347,7 @@ email address in the format `"Full Name <email>"`.
|
|
301
347
|
```ruby
|
302
348
|
def welcome_email(user)
|
303
349
|
@user = user
|
304
|
-
email_with_name = "#{@user.name} <#{@user.email}>
|
350
|
+
email_with_name = %("#{@user.name}" <#{@user.email}>)
|
305
351
|
mail(to: email_with_name, subject: 'Welcome to My Awesome Site')
|
306
352
|
end
|
307
353
|
```
|
@@ -317,7 +363,7 @@ for the HTML version and `welcome_email.text.erb` for the plain text version.
|
|
317
363
|
To change the default mailer view for your action you do something like:
|
318
364
|
|
319
365
|
```ruby
|
320
|
-
class UserMailer <
|
366
|
+
class UserMailer < ApplicationMailer
|
321
367
|
default from: 'notifications@example.com'
|
322
368
|
|
323
369
|
def welcome_email(user)
|
@@ -339,7 +385,7 @@ If you want more flexibility you can also pass a block and render specific
|
|
339
385
|
templates or even render inline or text without using a template file:
|
340
386
|
|
341
387
|
```ruby
|
342
|
-
class UserMailer <
|
388
|
+
class UserMailer < ApplicationMailer
|
343
389
|
default from: 'notifications@example.com'
|
344
390
|
|
345
391
|
def welcome_email(user)
|
@@ -369,7 +415,7 @@ layout.
|
|
369
415
|
In order to use a different file, call `layout` in your mailer:
|
370
416
|
|
371
417
|
```ruby
|
372
|
-
class UserMailer <
|
418
|
+
class UserMailer < ApplicationMailer
|
373
419
|
layout 'awesome' # use awesome.(html|text).erb as the layout
|
374
420
|
end
|
375
421
|
```
|
@@ -381,7 +427,7 @@ You can also pass in a `layout: 'layout_name'` option to the render call inside
|
|
381
427
|
the format block to specify different layouts for different formats:
|
382
428
|
|
383
429
|
```ruby
|
384
|
-
class UserMailer <
|
430
|
+
class UserMailer < ApplicationMailer
|
385
431
|
def welcome_email(user)
|
386
432
|
mail(to: user.email) do |format|
|
387
433
|
format.html { render layout: 'my_layout' }
|
@@ -406,6 +452,22 @@ globally in `config/application.rb`:
|
|
406
452
|
config.action_mailer.default_url_options = { host: 'example.com' }
|
407
453
|
```
|
408
454
|
|
455
|
+
Because of this behavior you cannot use any of the `*_path` helpers inside of
|
456
|
+
an email. Instead you will need to use the associated `*_url` helper. For example
|
457
|
+
instead of using
|
458
|
+
|
459
|
+
```
|
460
|
+
<%= link_to 'welcome', welcome_path %>
|
461
|
+
```
|
462
|
+
|
463
|
+
You will need to use:
|
464
|
+
|
465
|
+
```
|
466
|
+
<%= link_to 'welcome', welcome_url %>
|
467
|
+
```
|
468
|
+
|
469
|
+
By using the full URL, your links will now work in your emails.
|
470
|
+
|
409
471
|
#### generating URLs with `url_for`
|
410
472
|
|
411
473
|
You need to pass the `only_path: false` option when using `url_for`. This will
|
@@ -463,7 +525,7 @@ while delivering emails, you can do this using `delivery_method_options` in the
|
|
463
525
|
mailer action.
|
464
526
|
|
465
527
|
```ruby
|
466
|
-
class UserMailer <
|
528
|
+
class UserMailer < ApplicationMailer
|
467
529
|
def welcome_email(user, company)
|
468
530
|
@user = user
|
469
531
|
@url = user_url(@user)
|
@@ -485,7 +547,7 @@ option. In such cases don't forget to add the `:content_type` option. Rails
|
|
485
547
|
will default to `text/plain` otherwise.
|
486
548
|
|
487
549
|
```ruby
|
488
|
-
class UserMailer <
|
550
|
+
class UserMailer < ApplicationMailer
|
489
551
|
def welcome_email(user, email_body)
|
490
552
|
mail(to: user.email,
|
491
553
|
body: email_body,
|
@@ -515,7 +577,7 @@ mailer, and pass the email object to the mailer `receive` instance
|
|
515
577
|
method. Here's an example:
|
516
578
|
|
517
579
|
```ruby
|
518
|
-
class UserMailer <
|
580
|
+
class UserMailer < ApplicationMailer
|
519
581
|
def receive(email)
|
520
582
|
page = Page.find_by(address: email.to.first)
|
521
583
|
page.emails.create(
|
@@ -551,7 +613,7 @@ Action Mailer allows for you to specify a `before_action`, `after_action` and
|
|
551
613
|
using instance variables set in your mailer action.
|
552
614
|
|
553
615
|
```ruby
|
554
|
-
class UserMailer <
|
616
|
+
class UserMailer < ApplicationMailer
|
555
617
|
after_action :set_delivery_options,
|
556
618
|
:prevent_delivery_to_guests,
|
557
619
|
:set_business_headers
|
@@ -608,7 +670,7 @@ files (environment.rb, production.rb, etc...)
|
|
608
670
|
| Configuration | Description |
|
609
671
|
|---------------|-------------|
|
610
672
|
|`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>|
|
673
|
+
|`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
674
|
|`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
675
|
|`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
676
|
|`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 +679,7 @@ files (environment.rb, production.rb, etc...)
|
|
617
679
|
|`default_options`|Allows you to set default values for the `mail` method options (`:from`, `:reply_to`, etc.).|
|
618
680
|
|
619
681
|
For a complete writeup of possible configurations see the
|
620
|
-
[Action Mailer
|
682
|
+
[Configuring Action Mailer](configuring.html#configuring-action-mailer) in
|
621
683
|
our Configuring Rails Applications guide.
|
622
684
|
|
623
685
|
### Example Action Mailer Configuration
|
@@ -662,6 +724,7 @@ You can find detailed instructions on how to test your mailers in the
|
|
662
724
|
|
663
725
|
Intercepting Emails
|
664
726
|
-------------------
|
727
|
+
|
665
728
|
There are situations where you need to edit an email before it's
|
666
729
|
delivered. Fortunately Action Mailer provides hooks to intercept every
|
667
730
|
email. You can register an interceptor to make modifications to mail messages
|
@@ -685,5 +748,5 @@ ActionMailer::Base.register_interceptor(SandboxEmailInterceptor) if Rails.env.st
|
|
685
748
|
|
686
749
|
NOTE: The example above uses a custom environment called "staging" for a
|
687
750
|
production like server but for testing purposes. You can read
|
688
|
-
[Creating Rails environments](
|
751
|
+
[Creating Rails environments](configuring.html#creating-rails-environments)
|
689
752
|
for more information about custom Rails environments.
|