質問編集履歴

6

記入していなかったファイルの追加

2022/04/27 01:50

投稿

harukome
harukome

スコア26

test CHANGED
File without changes
test CHANGED
@@ -31,6 +31,14 @@
31
31
  mail(to: @likeduser.email, subject: 'あなたの投稿にいいねがつきました')
32
32
  end
33
33
  end
34
+ ```
35
+
36
+ ```application_mailer.rb
37
+ class ApplicationMailer < ActionMailer::Base
38
+ default from: "TestLife運営"
39
+ layout 'mailer'
40
+ end
41
+
34
42
  ```
35
43
  2,likesコントローラー
36
44
  ```likes_controller

5

説明が不足していた部分の追記

2022/04/26 00:55

投稿

harukome
harukome

スコア26

test CHANGED
File without changes
test CHANGED
@@ -137,7 +137,7 @@
137
137
  ```
138
138
 
139
139
  ### 試したこと
140
- 0. likeコントローラーの記述の変更
140
+ 0. 現在のコードからlikeコントローラーの記述の変更
141
141
  【変更前】
142
142
  ```
143
143
  NotificationMailer.send_when_liked.with(likeuser: @likeuser, likeduser: @likeduser, post: @post).deliver
@@ -152,7 +152,7 @@
152
152
  Net::SMTPFatalError in LikesController#create
153
153
  555 5.5.2 Syntax error. o11-20020a62cd0b000000b0050d3365725csm6429534pfg.105 - gsmtp
154
154
  ```
155
- 2. notification_mailerの記述の変更
155
+ 2. 現在のコードからnotification_mailerの記述の変更
156
156
  【変更前】
157
157
  ```ここに言語を入力
158
158
  def send_when_liked

4

現時点でのコードへ変更と試したことを追記しました。

2022/04/26 00:52

投稿

harukome
harukome

スコア26

test CHANGED
File without changes
test CHANGED
@@ -12,9 +12,11 @@
12
12
 
13
13
  ### 発生している問題・エラーメッセージ
14
14
 
15
- コマンドプロンプトには以下のエラーメッセージが表示されています。
15
+ 以下のエラーメッセージが表示されています。
16
+ notification_mailer.rbの@likeuser = params[:likeuser]の部分に赤線
16
- ```コマンドプロンプト
17
+ ```
18
+ NoMethodError in LikesController#create
17
- NoMethodError (undefined method `[]' for nil:NilClass):
19
+ undefined method `[]' for nil:NilClass
18
20
  ```
19
21
 
20
22
  ### 該当のソースコード
@@ -34,9 +36,9 @@
34
36
  ```likes_controller
35
37
  def create
36
38
  like = current_user.likes.create(post_id: params[:post_id])
37
- post = Post.find(params[:post_id]) #いいねされた投稿の取得
39
+ @post = Post.find(params[:post_id]) #いいねされた投稿の取得
38
- likeduser = User.find(post.user_id) #いいねされたユーザーの取得
40
+ @likeduser = User.find(post.user_id) #いいねされたユーザーの取得
39
- NotificationMailer.send_when_liked.with(likeuser: current_user, likeduser: likeduser, post: post).deliver
41
+ NotificationMailer.send_when_liked.with(likeuser: @likeuser, likeduser: @likeduser, post: @post).deliver
40
42
  redirect_back(fallback_location: root_path)
41
43
  end
42
44
  ```
@@ -107,35 +109,66 @@
107
109
  ### エラーメッセージ
108
110
  「いいね」ボタンを押した後のコマンドプロンプトは以下のように動きます。
109
111
  ```ここに言語を入力
110
- Started POST "/posts/2/likes" for ::1 at 2022-04-24 20:26:09 +0900
112
+ Started POST "/posts/2/likes" for ::1 at 2022-04-26 09:45:44 +0900
111
113
  Processing by LikesController#create as HTML
112
114
  Parameters: {"authenticity_token"=>"[FILTERED]", "post_id"=>"2"}
113
115
  User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
114
- TRANSACTION (0.1ms) begin transaction
116
+ TRANSACTION (0.3ms) begin transaction
115
117
  ↳ app/controllers/likes_controller.rb:3:in `create'
116
- Post Load (0.4ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
118
+ Post Load (0.7ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
117
119
  ↳ app/controllers/likes_controller.rb:3:in `create'
118
- Like Exists? (0.3ms) SELECT 1 AS one FROM "likes" WHERE "likes"."post_id" = ? AND "likes"."user_id" = ? LIMIT ? [["post_id", 2], ["user_id", 1], ["LIMIT", 1]]
120
+ Like Exists? (0.7ms) SELECT 1 AS one FROM "likes" WHERE "likes"."post_id" = ? AND "likes"."user_id" = ? LIMIT ? [["post_id", 2], ["user_id", 1], ["LIMIT", 1]]
119
121
  ↳ app/controllers/likes_controller.rb:3:in `create'
120
- Like Create (2.6ms) INSERT INTO "likes" ("post_id", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["post_id", 2], ["user_id", 1], ["created_at", "2022-04-24 11:26:09.319903"], ["updated_at", "2022-04-24 11:26:09.319903"]]
122
+ Like Create (4.2ms) INSERT INTO "likes" ("post_id", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["post_id", 2], ["user_id", 1], ["created_at", "2022-04-26 00:45:45.031631"], ["updated_at", "2022-04-26 00:45:45.031631"]]
121
123
  ↳ app/controllers/likes_controller.rb:3:in `create'
122
- TRANSACTION (6.5ms) commit transaction
124
+ TRANSACTION (10.2ms) commit transaction
123
125
  ↳ app/controllers/likes_controller.rb:3:in `create'
124
- Post Load (0.3ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
126
+ Post Load (0.7ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
125
127
  ↳ app/controllers/likes_controller.rb:4:in `create'
126
- User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 3], ["LIMIT", 1]]
128
+ User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 3], ["LIMIT", 1]]
127
129
  ↳ app/controllers/likes_controller.rb:6:in `create'
128
- NotificationMailer#send_when_liked: processed outbound mail in 8.9ms
130
+ NotificationMailer#send_when_liked: processed outbound mail in 5.4ms
129
- Completed 500 Internal Server Error in 130ms (ActiveRecord: 11.4ms | Allocations: 8335)
131
+ Completed 500 Internal Server Error in 507ms (ActiveRecord: 17.9ms | Allocations: 15891)
130
-
131
-
132
132
 
133
133
  NoMethodError (undefined method `[]' for nil:NilClass):
134
134
 
135
135
  app/mailers/notification_mailer.rb:9:in `send_when_liked'
136
136
  app/controllers/likes_controller.rb:7:in `create'
137
137
  ```
138
+
138
139
  ### 試したこと
140
+ 0. likeコントローラーの記述の変更
141
+ 【変更前】
142
+ ```
143
+ NotificationMailer.send_when_liked.with(likeuser: @likeuser, likeduser: @likeduser, post: @post).deliver
144
+ ```
145
+ 【変更後】
146
+ ```
147
+ NotificationMailer.with(likeuser: @likeuser, likeduser: @likeduser, post: @post).send_when_liked.deliver
148
+ ```
149
+ 【結果】
150
+ 以下のエラーメッセージが出ました
151
+ ```
152
+ Net::SMTPFatalError in LikesController#create
153
+ 555 5.5.2 Syntax error. o11-20020a62cd0b000000b0050d3365725csm6429534pfg.105 - gsmtp
154
+ ```
155
+ 2. notification_mailerの記述の変更
156
+ 【変更前】
157
+ ```ここに言語を入力
158
+ def send_when_liked
159
+ ```
160
+ 【変更後】
161
+ ```ここに言語を入力
162
+ def send_when_liked(likeuser, likeduser, post)
163
+ ```
164
+ 【結果】
165
+ 以下のエラーメッセージが出ました
166
+ ```ここに言語を入力
167
+ ArgumentError in LikesController#create
168
+ wrong number of arguments (given 0, expected 3)
169
+ ```
170
+
171
+ ### 参考サイト
139
172
  以下のサイトを参考に実装を試みました。
140
173
  - [Railsガイド Action Mailerの基礎](https://railsguides.jp/action_mailer_basics.html)
141
174
  - [Railsでメール自動配信機能をつくるまでの道程](https://qiita.com/Yama-to/items/823baf26bba3193712ea#6-%E5%AE%9F%E9%9A%9B%E3%81%AB%E8%A1%A8%E7%A4%BA%E3%82%92%E7%A2%BA%E8%AA%8D%E3%81%99%E3%82%8B)
@@ -145,13 +178,3 @@
145
178
  と表示されていたので、ユーザー情報などがパラメーターがコントローラー側に上手くわたっていないのかなと考えています。
146
179
 
147
180
 
148
- ### 補足情報
149
-
150
- Net::SMTPFatalErrorのエラーはlikesコントローラーの記述を以下のように変更すると無くなりました。
151
- ```変更前
152
- NotificationMailer.with(likeuser: current_user, likeduser: likeduser, post: post).send_when_liked.deliver
153
- ```
154
- ```変更後
155
- NotificationMailer.send_when_liked.with(likeuser: current_user, likeduser: likeduser, post: post).deliver
156
- ```
157
-

3

コマンドプロンプトの動きとエラーメッセージを追記しました

2022/04/24 11:28

投稿

harukome
harukome

スコア26

test CHANGED
File without changes
test CHANGED
@@ -104,7 +104,37 @@
104
104
  <%= @likeuser.name %>さんが貴方の書いた「<%= @post.title %>」に「いいね」をしました。
105
105
  早速////をclickして確認してみましょう
106
106
  ```
107
+ ### エラーメッセージ
108
+ 「いいね」ボタンを押した後のコマンドプロンプトは以下のように動きます。
109
+ ```ここに言語を入力
110
+ Started POST "/posts/2/likes" for ::1 at 2022-04-24 20:26:09 +0900
111
+ Processing by LikesController#create as HTML
112
+ Parameters: {"authenticity_token"=>"[FILTERED]", "post_id"=>"2"}
113
+ User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
114
+ TRANSACTION (0.1ms) begin transaction
115
+ ↳ app/controllers/likes_controller.rb:3:in `create'
116
+ Post Load (0.4ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
117
+ ↳ app/controllers/likes_controller.rb:3:in `create'
118
+ Like Exists? (0.3ms) SELECT 1 AS one FROM "likes" WHERE "likes"."post_id" = ? AND "likes"."user_id" = ? LIMIT ? [["post_id", 2], ["user_id", 1], ["LIMIT", 1]]
119
+ ↳ app/controllers/likes_controller.rb:3:in `create'
120
+ Like Create (2.6ms) INSERT INTO "likes" ("post_id", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["post_id", 2], ["user_id", 1], ["created_at", "2022-04-24 11:26:09.319903"], ["updated_at", "2022-04-24 11:26:09.319903"]]
121
+ ↳ app/controllers/likes_controller.rb:3:in `create'
122
+ TRANSACTION (6.5ms) commit transaction
123
+ ↳ app/controllers/likes_controller.rb:3:in `create'
124
+ Post Load (0.3ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
125
+ ↳ app/controllers/likes_controller.rb:4:in `create'
126
+ User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 3], ["LIMIT", 1]]
127
+ ↳ app/controllers/likes_controller.rb:6:in `create'
128
+ NotificationMailer#send_when_liked: processed outbound mail in 8.9ms
129
+ Completed 500 Internal Server Error in 130ms (ActiveRecord: 11.4ms | Allocations: 8335)
107
130
 
131
+
132
+
133
+ NoMethodError (undefined method `[]' for nil:NilClass):
134
+
135
+ app/mailers/notification_mailer.rb:9:in `send_when_liked'
136
+ app/controllers/likes_controller.rb:7:in `create'
137
+ ```
108
138
  ### 試したこと
109
139
  以下のサイトを参考に実装を試みました。
110
140
  - [Railsガイド Action Mailerの基礎](https://railsguides.jp/action_mailer_basics.html)

2

誤字の修正をしました

2022/04/24 08:32

投稿

harukome
harukome

スコア26

test CHANGED
File without changes
test CHANGED
@@ -36,7 +36,7 @@
36
36
  like = current_user.likes.create(post_id: params[:post_id])
37
37
  post = Post.find(params[:post_id]) #いいねされた投稿の取得
38
38
  likeduser = User.find(post.user_id) #いいねされたユーザーの取得
39
- NotificationMailer.send_when_liked.with(likeuser: current_user.id, likeduser: likeduser.id, post: post).deliver
39
+ NotificationMailer.send_when_liked.with(likeuser: current_user, likeduser: likeduser, post: post).deliver
40
40
  redirect_back(fallback_location: root_path)
41
41
  end
42
42
  ```
@@ -112,16 +112,16 @@
112
112
  エラー画面を確認すると、
113
113
  Parameters:
114
114
  {"_method"=>"post", "authenticity_token"=>"[FILTERED]", "post_id"=>"2"}
115
- と表示されていたので、ユーザー情報などがパラメーターがコントローラー側に上手く割ったっていないのかなと考えています。
115
+ と表示されていたので、ユーザー情報などがパラメーターがコントローラー側に上手くたっていないのかなと考えています。
116
116
 
117
117
 
118
118
  ### 補足情報
119
119
 
120
120
  Net::SMTPFatalErrorのエラーはlikesコントローラーの記述を以下のように変更すると無くなりました。
121
121
  ```変更前
122
- NotificationMailer.with(likeuser: current_user.id, likeduser: likeduser.id, post: post).send_when_liked.deliver
122
+ NotificationMailer.with(likeuser: current_user, likeduser: likeduser, post: post).send_when_liked.deliver
123
123
  ```
124
124
  ```変更後
125
- NotificationMailer.send_when_liked.with(likeuser: current_user.id, likeduser: likeduser.id, post: post).deliver
125
+ NotificationMailer.send_when_liked.with(likeuser: current_user, likeduser: likeduser, post: post).deliver
126
126
  ```
127
127
 

1

ひとつめのエラーが解決したため、書き直しました。

2022/04/24 08:19

投稿

harukome
harukome

スコア26

test CHANGED
@@ -1 +1 @@
1
- mailerを使って「いいね」の通知メールを送ろうとしているがエラーが出る(Net::SMTPFatalError)
1
+ mailerを使って「いいね」の通知メールを送ろうとしているがエラーが出る(NoMethodError (undefined method `[]' for nil:NilClass):
test CHANGED
@@ -14,10 +14,7 @@
14
14
 
15
15
  コマンドプロンプトには以下のエラーメッセージが表示されています。
16
16
  ```コマンドプロンプト
17
- Completed 500 Internal Server Error in 2402ms (ActiveRecord: 12.0ms | Allocations: 23231)
17
+ NoMethodError (undefined method `[]' for nil:NilClass):
18
-
19
- Net::SMTPFatalError (555 5.5.2 Syntax error. w11-20020a17090a460b00b001cd4989fedesm2294319pjg.42 - gsmtp
20
- ):
21
18
  ```
22
19
 
23
20
  ### 該当のソースコード
@@ -39,7 +36,7 @@
39
36
  like = current_user.likes.create(post_id: params[:post_id])
40
37
  post = Post.find(params[:post_id]) #いいねされた投稿の取得
41
38
  likeduser = User.find(post.user_id) #いいねされたユーザーの取得
42
- NotificationMailer.with(likeuser: current_user, likeduser: likeduser, post: post).send_when_liked.deliver
39
+ NotificationMailer.send_when_liked.with(likeuser: current_user.id, likeduser: likeduser.id, post: post).deliver
43
40
  redirect_back(fallback_location: root_path)
44
41
  end
45
42
  ```
@@ -112,5 +109,19 @@
112
109
  以下のサイトを参考に実装を試みました。
113
110
  - [Railsガイド Action Mailerの基礎](https://railsguides.jp/action_mailer_basics.html)
114
111
  - [Railsでメール自動配信機能をつくるまでの道程](https://qiita.com/Yama-to/items/823baf26bba3193712ea#6-%E5%AE%9F%E9%9A%9B%E3%81%AB%E8%A1%A8%E7%A4%BA%E3%82%92%E7%A2%BA%E8%AA%8D%E3%81%99%E3%82%8B)
112
+ エラー画面を確認すると、
113
+ Parameters:
114
+ {"_method"=>"post", "authenticity_token"=>"[FILTERED]", "post_id"=>"2"}
115
+ と表示されていたので、ユーザー情報などがパラメーターがコントローラー側に上手く割ったっていないのかなと考えています。
115
116
 
116
117
 
118
+ ### 補足情報
119
+
120
+ Net::SMTPFatalErrorのエラーはlikesコントローラーの記述を以下のように変更すると無くなりました。
121
+ ```変更前
122
+ NotificationMailer.with(likeuser: current_user.id, likeduser: likeduser.id, post: post).send_when_liked.deliver
123
+ ```
124
+ ```変更後
125
+ NotificationMailer.send_when_liked.with(likeuser: current_user.id, likeduser: likeduser.id, post: post).deliver
126
+ ```
127
+