如何将参数传递给邮件方法? Rails 6
How to pass params into a mailer method? Rails 6
我正在使用 Rails 6
和 ActionMailer
。每当他的 post 被评论时,我都会向用户发送一封邮件。这是我的代码:
app/mailers/comment_mailer.rb
class CommentMailer < ApplicationMailer
def comment_mail
@user = params[:user]
mail(to: @user.email, subject: "Comments")
end
end
app/controllers/comments_controller.rb
class CommentsController < ApplicationController
before_action :authenticate_user!
def create
comment = Comment.new(comment_params)
comment.user_id = current_user.id
if comment.save
CommentMailer.with(user: @user).comment_mail.deliver_now
redirect_to post_path(comment.post.id)
end
end
app/views/comment_mailer/comment_mail.haml
%h4
Hi
- @user.name
%p Someone commented your post! Click the link below to see it:
(I haven't done the link step yet)
学习本教程后:https://dev.to/morinoko/sending-emails-in-rails-with-action-mailer-and-gmail-35g4 我遇到了这个错误:
NoMethodError in CommentsController#create
undefined method `email' for nil:NilClass
我的主机:
NoMethodError (undefined method `email' for nil:NilClass):
app/mailers/comment_mailer.rb:5:in `comment_mail'
app/controllers/comments_controller.rb:8:in `create'
Started POST "/comments" for 172.17.0.1 at 2020-07-01 00:32:38 +0000
Cannot render console from 172.17.0.1! Allowed networks: 127.0.0.0/127.255.255.255, ::1
Processing by CommentsController#create as HTML
Parameters: {"authenticity_token"=>"Mf15U/OG9DX3SKwoBRHE/D/xENBocTtdcS07aUur+p/tGJAWSxYSP65kovzhLXHXBjvs/Wzp2dV4/1+L4nxrdQ==", "comment"=>{"content"=>"well said!", "post_id"=>"2"}, "commit"=>"Comment"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ORDER BY "users"."id" ASC LIMIT [["id", 1], ["LIMIT", 1]]
(0.3ms) BEGIN
↳ app/controllers/comments_controller.rb:7:in `create'
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = LIMIT [["id", 1], ["LIMIT", 1]]
↳ app/controllers/comments_controller.rb:7:in `create'
Post Load (0.4ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = LIMIT [["id", 2], ["LIMIT", 1]]
↳ app/controllers/comments_controller.rb:7:in `create'
Comment Create (0.7ms) INSERT INTO "comments" ("content", "user_id", "post_id", "created_at", "updated_at") VALUES (, , , , ) RETURNING "id" [["content", "well said!"], ["user_id", 1], ["post_id", 2], ["created_at", "2020-07-01 00:32:38.439626"], ["updated_at", "2020-07-01 00:32:38.439626"]]
↳ app/controllers/comments_controller.rb:7:in `create'
(1.1ms) COMMIT
↳ app/controllers/comments_controller.rb:7:in `create'
CommentMailer#comment_email: processed outbound mail in 1.4ms
Completed 500 Internal Server Error in 54ms (ActiveRecord: 3.6ms | Allocations: 29044)
我用 binding.pry
复习了 comment_mail
方法,我有 @user
>> nil
作为 return。
假设评论 belong_to
posts 或任何正在评论的内容,然后还假设 posts belong_to
是用户。您需要填充该 @user
变量。类似于:
@user = comment.post.user
获取 post 的用户被评论。
我认为这应该可行
CommentMailer.comment_mail(@user).deliver_now
然后更新方法本身
def comment_mail(user)
@user = user
mail(to: @user.email, subject: "Comments")
end
如果需要,您也应该能够在邮件视图模板中使用 @user
对象。
当你打电话给CommentMailer.with(user: @user).comment_mail.deliver_now
你还没有真正定义 @user
你提到你有一个用户附加到 post 记录所以使用那个
comment.post.user
所以现在你有
CommentMailer.with(user: comment.post.user).comment_mail.deliver_now
或者你让用户成为comment_mail
的参数
class CommentMailer < ApplicationMailer
def comment_mail(user)
@user = user #and this makes @user available in your templates
mail(to: @user.email, subject: "Comments")
end
end
正如您所期望的那样称呼它
CommentMailer.comment_mail(comment.post.user).deliver_now #or preferably deliver_later
我正在使用 Rails 6
和 ActionMailer
。每当他的 post 被评论时,我都会向用户发送一封邮件。这是我的代码:
app/mailers/comment_mailer.rb
class CommentMailer < ApplicationMailer
def comment_mail
@user = params[:user]
mail(to: @user.email, subject: "Comments")
end
end
app/controllers/comments_controller.rb
class CommentsController < ApplicationController
before_action :authenticate_user!
def create
comment = Comment.new(comment_params)
comment.user_id = current_user.id
if comment.save
CommentMailer.with(user: @user).comment_mail.deliver_now
redirect_to post_path(comment.post.id)
end
end
app/views/comment_mailer/comment_mail.haml
%h4
Hi
- @user.name
%p Someone commented your post! Click the link below to see it:
(I haven't done the link step yet)
学习本教程后:https://dev.to/morinoko/sending-emails-in-rails-with-action-mailer-and-gmail-35g4 我遇到了这个错误:
NoMethodError in CommentsController#create
undefined method `email' for nil:NilClass
我的主机:
NoMethodError (undefined method `email' for nil:NilClass):
app/mailers/comment_mailer.rb:5:in `comment_mail'
app/controllers/comments_controller.rb:8:in `create'
Started POST "/comments" for 172.17.0.1 at 2020-07-01 00:32:38 +0000
Cannot render console from 172.17.0.1! Allowed networks: 127.0.0.0/127.255.255.255, ::1
Processing by CommentsController#create as HTML
Parameters: {"authenticity_token"=>"Mf15U/OG9DX3SKwoBRHE/D/xENBocTtdcS07aUur+p/tGJAWSxYSP65kovzhLXHXBjvs/Wzp2dV4/1+L4nxrdQ==", "comment"=>{"content"=>"well said!", "post_id"=>"2"}, "commit"=>"Comment"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ORDER BY "users"."id" ASC LIMIT [["id", 1], ["LIMIT", 1]]
(0.3ms) BEGIN
↳ app/controllers/comments_controller.rb:7:in `create'
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = LIMIT [["id", 1], ["LIMIT", 1]]
↳ app/controllers/comments_controller.rb:7:in `create'
Post Load (0.4ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = LIMIT [["id", 2], ["LIMIT", 1]]
↳ app/controllers/comments_controller.rb:7:in `create'
Comment Create (0.7ms) INSERT INTO "comments" ("content", "user_id", "post_id", "created_at", "updated_at") VALUES (, , , , ) RETURNING "id" [["content", "well said!"], ["user_id", 1], ["post_id", 2], ["created_at", "2020-07-01 00:32:38.439626"], ["updated_at", "2020-07-01 00:32:38.439626"]]
↳ app/controllers/comments_controller.rb:7:in `create'
(1.1ms) COMMIT
↳ app/controllers/comments_controller.rb:7:in `create'
CommentMailer#comment_email: processed outbound mail in 1.4ms
Completed 500 Internal Server Error in 54ms (ActiveRecord: 3.6ms | Allocations: 29044)
我用 binding.pry
复习了 comment_mail
方法,我有 @user
>> nil
作为 return。
假设评论 belong_to
posts 或任何正在评论的内容,然后还假设 posts belong_to
是用户。您需要填充该 @user
变量。类似于:
@user = comment.post.user
获取 post 的用户被评论。
我认为这应该可行
CommentMailer.comment_mail(@user).deliver_now
然后更新方法本身
def comment_mail(user)
@user = user
mail(to: @user.email, subject: "Comments")
end
如果需要,您也应该能够在邮件视图模板中使用 @user
对象。
当你打电话给CommentMailer.with(user: @user).comment_mail.deliver_now
你还没有真正定义 @user
你提到你有一个用户附加到 post 记录所以使用那个
comment.post.user 所以现在你有
CommentMailer.with(user: comment.post.user).comment_mail.deliver_now
或者你让用户成为comment_mail
的参数class CommentMailer < ApplicationMailer
def comment_mail(user)
@user = user #and this makes @user available in your templates
mail(to: @user.email, subject: "Comments")
end
end
正如您所期望的那样称呼它
CommentMailer.comment_mail(comment.post.user).deliver_now #or preferably deliver_later