teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

コードを追加しました。

2021/06/20 08:38

投稿

y-kou
y-kou

スコア7

title CHANGED
File without changes
body CHANGED
@@ -1,10 +1,4 @@
1
- rails チュートリアル11章のメイラーの部分をやっています。applicatoin_mailer.rbのdefault from: の部分を変えてもプレビューで反映されません。どのようにすればよいでしょうか?
1
+ rails チュートリアル11章のメイラーの部分をやっています。applicatoin_mailer.rbのdefault from: の部分を変えてもプレビューで反映されませんでした。
2
-
3
- > mailer生成
4
- ```
5
- rails generate mailer UserMailer account_activation password_reset
6
- ```
7
-
8
2
  ![イメージ説明](f9c3ade4042e4f61b5cfe5dc621a4fe0.png)
9
3
  > application_mailer.rb
10
4
  ```ruby
@@ -31,37 +25,47 @@
31
25
  end
32
26
 
33
27
  ```
28
+ その後、user_mailer.rbで個別にfrom:で設定したところ、問題なく動作しました。
29
+ なぜdefault from: では反映されなかったのでしょうか?
30
+ ![![イメージ説明](c055b795808a9a31a7afbb8bcf1ac22d.png)
34
- > user_mailer_preview.rb
31
+ > application_mailer.rb
35
32
  ```ruby
36
- # Preview all emails at http://localhost:3000/rails/mailers/user_mailer
37
- class UserMailerPreview < ActionMailer::Preview
33
+ class ApplicationMailer < ActionMailer::Base
34
+ layout 'mailer'
35
+ end
36
+ ```
37
+ > user_mailer.rb
38
+ ```ruby
39
+ class UserMailer < ApplicationMailer
38
40
 
39
- # Preview this email at
40
- # http://localhost:3000/rails/mailers/user_mailer/account_activation
41
+ # Subject can be set in your I18n file at config/locales/en.yml
42
+ # with the following lookup:
43
+ #
44
+ # en.user_mailer.account_activation.subject
45
+ #
41
- def account_activation
46
+ def account_activation(user)
42
- user = User.first
47
+ @user = user
43
- user.activation_token = User.new_token
48
+ mail from: "noreply@example.com",
49
+ to: user.email,
44
- UserMailer.account_activation(user)
50
+ subject: 'Account activation'
45
51
  end
46
52
 
47
- # Preview this email at
48
- # http://localhost:3000/rails/mailers/user_mailer/password_reset
53
+ # Subject can be set in your I18n file at config/locales/en.yml
54
+ # with the following lookup:
55
+ #
56
+ # en.user_mailer.password_reset.subject
57
+ #
49
58
  def password_reset
50
- UserMailer.password_reset
59
+ @greeting = "Hi"
60
+
61
+ mail to: "to@example.org"
51
62
  end
52
63
  end
64
+
53
65
  ```
54
- > account_activation.html.erb
55
- ```ruby
56
- <h1>Sample App</h1>
57
66
 
58
- <p>Hi <%= @user.name %>,</p>
59
67
 
60
- <p>
61
- Welcome to the Sample App! Click on the link below to activate your account:
62
- </p>
68
+ > mailer生成
63
-
69
+ ```
64
- <%= link_to "Activate", edit_account_activation_url(@user.activation_token,
70
+ rails generate mailer UserMailer account_activation password_reset
65
- email: @user.email) %>
66
-
67
71
  ```