gpt4 book ai didi

ruby-on-rails - 如何将参数传递到邮件程序方法中? rails 6

转载 作者:行者123 更新时间:2023-12-03 08:41:22 25 4
gpt4 key购买 nike

我正在使用 Rails 6ActionMailer。我正在努力在每次评论用户的帖子时向他发送一封邮件。这是我的代码:

应用程序/mailers/comment_mailer.rb

class CommentMailer < ApplicationMailer
def comment_mail
@user = params[:user]

mail(to: @user.email, subject: "Comments")
end
end

应用程序/ Controller /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" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["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" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ app/controllers/comments_controller.rb:7:in `create'
Post Load (0.4ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT $2 [["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 ($1, $2, $3, $4, $5) 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 作为返回。

最佳答案

当您调用CommentMailer.with(user: @user).comment_mail.deliver_now您实际上尚未定义 @user 您提到您有一个用户附加到帖子记录,因此请使用它

评论.帖子.用户所以现在你有了

CommentMailer.with(user: comment.post.user).comment_mail.deliver_now

或者你将user作为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

关于ruby-on-rails - 如何将参数传递到邮件程序方法中? rails 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62668676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com