ActionMailer将局部变量传递给erb模板

时间:2021-11-05 22:30:35

I know I could define instance variables e.g:

我知道我可以定义实例变量,例如:

def user_register(username, email)
  @username = username
  @email = email

  mail(:to => email, :subject => "Welcome!", :template_name => "reg_#{I18n.locale}")
end

But, is there a way to use local variables instead, just like passing :locals to partials?

但是,是否有一种方法可以使用局部变量,就像传递:locals到partials一样?

4 个解决方案

#1


8  

All options available in the mail method can be found at http://api.rubyonrails.org/classes/ActionMailer/Base.html#method-i-mail.

邮件方法中提供的所有选项均可在http://api.rubyonrails.org/classes/ActionMailer/Base.html#method-i-mail中找到。

We know that render has a :locals option. However we can see that there is no :locals option available for mail. Therefore, no, there is no better way than to use instance variables (unless you want to use something hideous like globals or persistent database objects - don't do this).

我们知道render有一个:locals选项。但是我们可以看到没有:locals选项可用于邮件。因此,不,没有比使用实例变量更好的方法(除非你想使用像全局变量或持久数据库对象一样可怕的东西 - 不要这样做)。

Instance variables are what you are meant to use.

实例变量就是您要使用的。

#2


15  

As ronalchn pointed out, it's the render that has :locals, not the mail method. So, you need a direct access to the render method in order to pass the locals.

正如ronalchn所指出的那样,渲染具有:本地人,而不是邮件方法。因此,您需要直接访问render方法才能传递本地。

You can give a block to the mail and that way gain access to the render method, something like this:

你可以给邮件一个块,这样就可以访问render方法,如下所示:

mail(to: "your_mail@example.com", subject: "Test passing locals to view from mailer") do |format|
  format.html {
    render locals: { recipient_name: "John D." }
  }
end

And now you should be able to use "Hello <%= recipient_name %>"

现在你应该可以使用“Hello <%= recipient_name%>”

#3


1  

You can actually use the locals option with mail, it's just a bit confusing and inconsistent as to how.

你实际上可以使用locals选项和邮件,它只是有点混乱和如何不一致。

Once you use :locals you can then access these locals in the mail template using instance variables, e.g.

一旦您使用:本地人,您就可以使用实例变量访问邮件模板中的这些本地人,例如:

:locals => { :name => 'Jane' }

and then in the template

然后在模板中

Dear <%= @name %>,

#4


0  

In Rails 5, you simply have to define instance variables using @ in your method. You no longer have access to the locals property for this purpose.

在Rails 5中,您只需在方法中使用@定义实例变量。您无法再为此目的访问locals属性。

class UserMailer < ApplicationMailer

  def welcome_email(user_id:, to_email:, user_full_name:, token:)    
    # Mail template variables
    @user = User.find_by(id: user_id)
    @token = token

    mail(:to => to_email,
       :subject => MAILER_SUBJECTS_WELCOME,
       :template_path => "user_mailer",
       :template_name => "welcome_email")
  end
end

Then you can just access them in your email template using <%= @user %> and <%= @token %>

然后,您只需使用<%= @ user%>和<%= @token%>在电子邮件模板中访问它们

#1


8  

All options available in the mail method can be found at http://api.rubyonrails.org/classes/ActionMailer/Base.html#method-i-mail.

邮件方法中提供的所有选项均可在http://api.rubyonrails.org/classes/ActionMailer/Base.html#method-i-mail中找到。

We know that render has a :locals option. However we can see that there is no :locals option available for mail. Therefore, no, there is no better way than to use instance variables (unless you want to use something hideous like globals or persistent database objects - don't do this).

我们知道render有一个:locals选项。但是我们可以看到没有:locals选项可用于邮件。因此,不,没有比使用实例变量更好的方法(除非你想使用像全局变量或持久数据库对象一样可怕的东西 - 不要这样做)。

Instance variables are what you are meant to use.

实例变量就是您要使用的。

#2


15  

As ronalchn pointed out, it's the render that has :locals, not the mail method. So, you need a direct access to the render method in order to pass the locals.

正如ronalchn所指出的那样,渲染具有:本地人,而不是邮件方法。因此,您需要直接访问render方法才能传递本地。

You can give a block to the mail and that way gain access to the render method, something like this:

你可以给邮件一个块,这样就可以访问render方法,如下所示:

mail(to: "your_mail@example.com", subject: "Test passing locals to view from mailer") do |format|
  format.html {
    render locals: { recipient_name: "John D." }
  }
end

And now you should be able to use "Hello <%= recipient_name %>"

现在你应该可以使用“Hello <%= recipient_name%>”

#3


1  

You can actually use the locals option with mail, it's just a bit confusing and inconsistent as to how.

你实际上可以使用locals选项和邮件,它只是有点混乱和如何不一致。

Once you use :locals you can then access these locals in the mail template using instance variables, e.g.

一旦您使用:本地人,您就可以使用实例变量访问邮件模板中的这些本地人,例如:

:locals => { :name => 'Jane' }

and then in the template

然后在模板中

Dear <%= @name %>,

#4


0  

In Rails 5, you simply have to define instance variables using @ in your method. You no longer have access to the locals property for this purpose.

在Rails 5中,您只需在方法中使用@定义实例变量。您无法再为此目的访问locals属性。

class UserMailer < ApplicationMailer

  def welcome_email(user_id:, to_email:, user_full_name:, token:)    
    # Mail template variables
    @user = User.find_by(id: user_id)
    @token = token

    mail(:to => to_email,
       :subject => MAILER_SUBJECTS_WELCOME,
       :template_path => "user_mailer",
       :template_name => "welcome_email")
  end
end

Then you can just access them in your email template using <%= @user %> and <%= @token %>

然后,您只需使用<%= @ user%>和<%= @token%>在电子邮件模板中访问它们