传递变量到Rails梅勒
问题描述:
嘿堆栈用户,传递变量到Rails梅勒
我一直在沮丧这个问题了大约两个小时,不知道我做错了。我在User
的更新操作上调用方法send_intro_email
。 send_intro_email
方法调用名为intro_email(user)
的NotificationMailer
方法。
我遇到的问题是user
的数据正在被一直传递,直到它在.erb电子邮件中使用的地步。我测试了它,它实际上正在通过,但不知道为什么我不能在电子邮件内部使用它。
文件:
notificaion_mailer.rb
def intro_email(user)
puts user # works
mail(to: "email", from: "email2", subject: "THIS IS A TEST")
end
User.rb
def send_intro_email
NotificationMailer.intro_email(self).deliver
end
intro_email.erb
The users name is <%= user.name %>.
我可以显示一个错误如果需要的话,当我登上我的另一台电脑时会有消息。非常感谢你的帮助。
答
那么,你使用的是局部变量user
,它的作用范围仅限于你定义的方法。您必须使用实例变量@user
作为示例。所以改变是这样做的:
def intro_email(user)
@user = user
mail(to: "email", from: "email2", subject: "THIS IS A TEST")
end
然后在视图内使用@user
。像
The users name is <%= @user.name %>.
嘿奥雅纳,我其实没有尝试,我没有收到错误,但它输出@ user.name而不是“鲍勃”或任何邮件发送时。 –
@ Matt-L将名称更改为intro_email.text.erb(这是用于文本电子邮件)。试着回到我身边 –
我会尝试,当我回家,让你知道它是如何工作的。谢谢 –