首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将params传递给mailer方法?Rails 6

如何将params传递给mailer方法?Rails 6
EN

Stack Overflow用户
提问于 2020-07-01 10:18:29
回答 2查看 1K关注 0票数 0

我使用的是Rails 6ActionMailer。每次他的帖子被评论的时候,我都会给他发一封邮件。下面是我的代码:

app/mailers/comment_mailer.rb

代码语言:javascript
复制
class CommentMailer < ApplicationMailer
  def comment_mail
    @user = params[:user]

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

应用程序/控制器/注释_控制器.rb

代码语言:javascript
复制
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

代码语言:javascript
复制
%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之后,我遇到了以下错误:

代码语言:javascript
复制
NoMethodError in CommentsController#create
undefined method `email' for nil:NilClass

我的控制台:

代码语言:javascript
复制
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作为返回值。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-07-01 11:21:19

当您调用CommentMailer.with(user: @user).comment_mail.deliver_now时,您还没有实际定义@user,您提到有一个用户附加到post记录,所以请使用它

comment.post.user所以现在你已经有了

代码语言:javascript
复制
CommentMailer.with(user: comment.post.user).comment_mail.deliver_now

或者让user成为comment_mail的一个参数

代码语言:javascript
复制
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

正如你所料,它是这样命名的

代码语言:javascript
复制
CommentMailer.comment_mail(comment.post.user).deliver_now #or preferably deliver_later
票数 2
EN

Stack Overflow用户

发布于 2020-07-01 11:13:40

假设评论是发布的,或者任何正在被评论的东西,然后还假设发布了belong_to a user。您需要填充该@user变量。类似于:

代码语言:javascript
复制
@user = comment.post.user

来获得被评论的帖子的用户。

我想这应该行得通。

代码语言:javascript
复制
CommentMailer.comment_mail(@user).deliver_now

,然后更新方法本身。

代码语言:javascript
复制
def comment_mail(user)
  @user = user

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

如果需要,您还应该能够在邮件视图模板中使用@user对象。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62668676

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档