[英]What are the variables accessible from user_mailer.html.erb?
我的user_mailer.rb:
class UserMailer < ActionMailer::Base
default :from => "notifications@example.com"
def welcome_email(user)
@user = user
@url = "http://example.com/login"
mail(:to => user.email,:subject => "Welcome to My Awesome Site") do |format|
format.html
end
end
end
这就是我的user_mailer.html.erb的样子:
<!DOCTYPE html>
<html>
<body>
<%= yield %>
</body>
</html>
我可以在user_mailer.html.erb中访问什么。 我需要在哪里定义环境变量,以便可以在此处访问它?
我猜你是从这里开始效法的吗?
你可以把user_mailer.html.erb
像视图, user_mailer.rb
喜欢它的控制器。 因此,如果您已定义实例变量@users
则可以在邮件中使用它。 按照链接中的示例:
user_mailer.html.erb
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
</head>
<body>
<h1>Welcome to example.com, <%= @user.name %></h1>
<p>
You have successfully signed up to example.com,
your username is: <%= @user.login %>.<br/>
</p>
<p>
To login to the site, just follow this link: <%= @url %>.
</p>
<p>Thanks for joining and have a great day!</p>
</body>
</html>
您也可以使用辅助程序,因此没有理由像这样对URL进行硬编码,而是可以使用sign_in_url
(或用于登录路径的任何路由)。
请注意,在电子邮件中,您应始终使用something_url
而不是something_path
( root_url
等)。
您应该在welcome_email
方法中定义变量:
def welcome_email(user)
@user = user
@time = Time.now
@slogan = "My App's slogan"
...
mail(:to => user.email,:subject => "Welcome to My Awesome Site") do |format|
format.html
end
end
您可以在视图部分访问任何实例变量。 所以就你而言
@user and @url are accessible in user_mailer.html.erb
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.