2015-08-14 85 views
0

嘿堆栈用户,传递变量到Rails梅勒

我一直在沮丧这个问题了大约两个小时,不知道我做错了。我在User的更新操作上调用方法send_intro_emailsend_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 %>. 

我可以显示一个错误如果需要的话,当我登上我的另一台电脑时会有消息。非常感谢你的帮助。

回答

0

那么,你使用的是局部变量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 %>. 
+0

嘿奥雅纳,我其实没有尝试,我没有收到错误,但它输出@ user.name而不是“鲍勃”或任何邮件发送时。 –

+0

@ Matt-L将名称更改为intro_email.text.erb(这是用于文本电子邮件)。试着回到我身边 –

+0

我会尝试,当我回家,让你知道它是如何工作的。谢谢 –