rails 4.1.14.2 → 4.2.11.1
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 +34 -37
- 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 +1 -1
- data/guides/bug_report_templates/action_controller_master.rb +1 -0
- data/guides/bug_report_templates/active_record_gem.rb +1 -1
- data/guides/bug_report_templates/generic_gem.rb +1 -1
- 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 +5 -2
- data/guides/source/3_2_release_notes.md +6 -3
- data/guides/source/4_0_release_notes.md +6 -3
- data/guides/source/4_1_release_notes.md +5 -6
- 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 +25 -8
- data/guides/source/action_mailer_basics.md +97 -29
- data/guides/source/action_view_overview.md +142 -183
- 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} +95 -220
- data/guides/source/active_record_postgresql.md +433 -0
- data/guides/source/active_record_querying.md +264 -268
- data/guides/source/active_record_validations.md +23 -13
- data/guides/source/active_support_core_extensions.md +114 -75
- data/guides/source/active_support_instrumentation.md +10 -18
- data/guides/source/api_documentation_guidelines.md +63 -17
- data/guides/source/asset_pipeline.md +258 -119
- 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 +440 -286
- data/guides/source/development_dependencies_install.md +47 -36
- data/guides/source/documents.yaml +19 -7
- data/guides/source/engines.md +189 -189
- data/guides/source/form_helpers.md +79 -56
- data/guides/source/generators.md +24 -11
- data/guides/source/getting_started.md +359 -219
- data/guides/source/i18n.md +110 -66
- data/guides/source/index.html.erb +1 -0
- data/guides/source/initialization.md +109 -62
- data/guides/source/layout.html.erb +5 -11
- data/guides/source/layouts_and_rendering.md +26 -26
- 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 -5
- data/guides/source/routing.md +113 -73
- data/guides/source/ruby_on_rails_guides_guidelines.md +11 -12
- data/guides/source/security.md +39 -33
- data/guides/source/testing.md +199 -119
- data/guides/source/upgrading_ruby_on_rails.md +287 -29
- data/guides/source/working_with_javascript_in_rails.md +19 -17
- data/guides/w3c_validator.rb +2 -0
- metadata +40 -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
@@ -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,14 @@
|
|
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
|
19
|
-
</
|
20
|
-
<
|
21
|
-
|
22
|
-
</
|
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:
|
19
|
+
<a href="http://guides.rubyonrails.org/v4.1/">Rails 4.1</a>,
|
20
|
+
<a href="http://guides.rubyonrails.org/v4.0/">Rails 4.0</a>,
|
21
|
+
<a href="http://guides.rubyonrails.org/v3.2/">Rails 3.2</a>, and
|
22
|
+
<a href="http://guides.rubyonrails.org/v2.3/">Rails 2.3</a>.
|
25
23
|
</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.
|
@@ -720,7 +735,7 @@ You can choose not to yield and build the response yourself, in which case the a
|
|
720
735
|
|
721
736
|
While the most common way to use filters is by creating private methods and using *_action to add them, there are two other ways to do the same thing.
|
722
737
|
|
723
|
-
The first is to use a block directly with the
|
738
|
+
The first is to use a block directly with the *\_action methods. The block receives the controller as an argument, and the `require_login` filter from above could be rewritten to use a block:
|
724
739
|
|
725
740
|
```ruby
|
726
741
|
class ApplicationController < ActionController::Base
|
@@ -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,6 +1164,8 @@ 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).
|
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
|
|
1154
1171
|
Force HTTPS protocol
|
@@ -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: "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
|
@@ -445,6 +507,9 @@ url helper.
|
|
445
507
|
<%= user_url(@user, host: 'example.com') %>
|
446
508
|
```
|
447
509
|
|
510
|
+
NOTE: non-`GET` links require [jQuery UJS](https://github.com/rails/jquery-ujs)
|
511
|
+
and won't work in mailer templates. They will result in normal `GET` requests.
|
512
|
+
|
448
513
|
### Sending Multipart Emails
|
449
514
|
|
450
515
|
Action Mailer will automatically send multipart emails if you have different
|
@@ -463,7 +528,7 @@ while delivering emails, you can do this using `delivery_method_options` in the
|
|
463
528
|
mailer action.
|
464
529
|
|
465
530
|
```ruby
|
466
|
-
class UserMailer <
|
531
|
+
class UserMailer < ApplicationMailer
|
467
532
|
def welcome_email(user, company)
|
468
533
|
@user = user
|
469
534
|
@url = user_url(@user)
|
@@ -485,7 +550,7 @@ option. In such cases don't forget to add the `:content_type` option. Rails
|
|
485
550
|
will default to `text/plain` otherwise.
|
486
551
|
|
487
552
|
```ruby
|
488
|
-
class UserMailer <
|
553
|
+
class UserMailer < ApplicationMailer
|
489
554
|
def welcome_email(user, email_body)
|
490
555
|
mail(to: user.email,
|
491
556
|
body: email_body,
|
@@ -515,7 +580,7 @@ mailer, and pass the email object to the mailer `receive` instance
|
|
515
580
|
method. Here's an example:
|
516
581
|
|
517
582
|
```ruby
|
518
|
-
class UserMailer <
|
583
|
+
class UserMailer < ApplicationMailer
|
519
584
|
def receive(email)
|
520
585
|
page = Page.find_by(address: email.to.first)
|
521
586
|
page.emails.create(
|
@@ -551,7 +616,7 @@ Action Mailer allows for you to specify a `before_action`, `after_action` and
|
|
551
616
|
using instance variables set in your mailer action.
|
552
617
|
|
553
618
|
```ruby
|
554
|
-
class UserMailer <
|
619
|
+
class UserMailer < ApplicationMailer
|
555
620
|
after_action :set_delivery_options,
|
556
621
|
:prevent_delivery_to_guests,
|
557
622
|
:set_business_headers
|
@@ -608,7 +673,7 @@ files (environment.rb, production.rb, etc...)
|
|
608
673
|
| Configuration | Description |
|
609
674
|
|---------------|-------------|
|
610
675
|
|`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
|
676
|
+
|`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` (will send the password in the clear), `:login` (will send password Base64 encoded) or `:cram_md5` (combines a Challenge/Response mechanism to exchange information and a cryptographic Message Digest 5 algorithm to hash important information)</li><li>`:enable_starttls_auto` - Detects if STARTTLS is enabled in your SMTP server and starts to use it. Defaults to `true`.</li><li>`:openssl_verify_mode` - When using TLS, you can set how OpenSSL checks the certificate. This is really useful if you need to validate a self-signed and/or a wildcard certificate. You can use the name of an OpenSSL verify constant ('none', 'peer', 'client_once', 'fail_if_no_peer_cert') or directly the constant (`OpenSSL::SSL::VERIFY_NONE`, `OpenSSL::SSL::VERIFY_PEER`, ...).</li></ul>|
|
612
677
|
|`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
678
|
|`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
679
|
|`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 +682,7 @@ files (environment.rb, production.rb, etc...)
|
|
617
682
|
|`default_options`|Allows you to set default values for the `mail` method options (`:from`, `:reply_to`, etc.).|
|
618
683
|
|
619
684
|
For a complete writeup of possible configurations see the
|
620
|
-
[Action Mailer
|
685
|
+
[Configuring Action Mailer](configuring.html#configuring-action-mailer) in
|
621
686
|
our Configuring Rails Applications guide.
|
622
687
|
|
623
688
|
### Example Action Mailer Configuration
|
@@ -662,6 +727,7 @@ You can find detailed instructions on how to test your mailers in the
|
|
662
727
|
|
663
728
|
Intercepting Emails
|
664
729
|
-------------------
|
730
|
+
|
665
731
|
There are situations where you need to edit an email before it's
|
666
732
|
delivered. Fortunately Action Mailer provides hooks to intercept every
|
667
733
|
email. You can register an interceptor to make modifications to mail messages
|
@@ -680,10 +746,12 @@ Mailer framework. You can do this in an initializer file
|
|
680
746
|
`config/initializers/sandbox_email_interceptor.rb`
|
681
747
|
|
682
748
|
```ruby
|
683
|
-
|
749
|
+
if Rails.env.staging?
|
750
|
+
ActionMailer::Base.register_interceptor(SandboxEmailInterceptor)
|
751
|
+
end
|
684
752
|
```
|
685
753
|
|
686
754
|
NOTE: The example above uses a custom environment called "staging" for a
|
687
755
|
production like server but for testing purposes. You can read
|
688
|
-
[Creating Rails environments](
|
756
|
+
[Creating Rails environments](configuring.html#creating-rails-environments)
|
689
757
|
for more information about custom Rails environments.
|