简体   繁体   English

ActionMailer 参数未通过预览

[英]ActionMailer Params not passing in preview

I'm new to Rails Action Mailer and when configring my first email, I get this error after visit http://localhost:3000/rails/mailers/user_mailer/confirm_email:我是 Rails Action Mailer 的新手,在配置我的第一个 email 时,访问 http://localhost:3000/rails/mailers/user_mailer/confirm_email 后出现此错误:

undefined method '[]' for nil:NilClass @user = params[:user] nil:NilClass @user = params[:user] 的未定义方法“[]”

My code:我的代码:

class UserMailer < ApplicationMailer
  def confirm_email
    @user = params[:user]
    mail to: "to@example.org"
  end
end

class UserMailerPreview < ActionMailer::Preview
  def confirm_email
    UserMailer.with(user: User.first).confirm_email
  end
end

If I change code to this:如果我将代码更改为此:

class UserMailer < ApplicationMailer
  def confirm_email(user)
    @user = user
    mail to: "to@example.org"
  end
end

class UserMailerPreview < ActionMailer::Preview
  def confirm_email
    UserMailer.confirm_email(User.last)
  end
end

The preview class is located in test/mailers/preview, outside of the app folder.预览版 class 位于 app 文件夹外的 test/mailers/preview 中。

I receive this:我收到这个:

wrong number of arguments (given 0, expected 1)

I cant understand, what I'm doing wrong?我无法理解,我做错了什么?

My rails version is 6.0.3.5我的导轨版本是 6.0.3.5

Provided you are positive you have at least one User record in your database, I would try the following, which is a mix of your attempts.如果您确定您的数据库中至少有一个用户记录,我会尝试以下方法,这是您尝试的混合。

class UserMailer < ApplicationMailer
  def confirm_email(user)
    @user = user
    mail to: "to@example.org"
  end
end

class UserMailerPreview < ActionMailer::Preview
  def confirm_email
    UserMailer.with(user: User.first).confirm_email
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM