ActionMailer 视图中的 url_for 和命名路由出现问题:“需要控制器和操作”
我试图在我的用户欢迎电子邮件中提供确认链接,但收到以下 Rails 错误:
Need controller and action!
It made a fuss about this line:
<p>Please take a moment to activate your account by going to:
<%= link_to confirm_user_url(:id => @user.confirmation_code) %>.</p>
在我的 development.rb
环境中,我有以下内容line:
config.action_mailer.default_url_options = {
:host => "localhost", :port => 3000
}
@user
变量没有问题。我已经使用 @user.username
和 @user.confirmation_code
等内容测试了电子邮件。我只是在 url_for
和像 confirm_user_url
这样的命名路由方面遇到了麻烦。
当我使用rake paths
检查路由时,会显示confirm_user
,因此这不是命名路由不存在的问题。
我似乎无法弄清楚。什么给?
I'm attempting to provide a confirmation link in my user welcome email and I'm getting the following Rails error:
Need controller and action!
It makes a fuss about this line:
<p>Please take a moment to activate your account by going to:
<%= link_to confirm_user_url(:id => @user.confirmation_code) %>.</p>
In my development.rb
environment, I have the following line:
config.action_mailer.default_url_options = {
:host => "localhost", :port => 3000
}
There's no problem with the @user
variable. I've tested the email with things like @user.username
and @user.confirmation_code
. I'm only getting trouble with url_for
and named routes like confirm_user_url
.
When I check my routes with rake routes
, confirm_user
shows up, so it's not an issue with the named route not existing.
I can't seem to figure it out. What gives?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ActionMailer 不是真正的 Rails 控制器,因此路由在邮件程序模板中不可用。您需要在邮件程序类中设置实例变量以将值传递给模板。
例如。如果您的邮件程序是 SampleMailer:
则在模板中:
这在“ActionMailer”中的 api 中进行了一些神秘的解释部分,以及 使用 Rails 进行敏捷 Web 开发的更多详细信息,第三版,第 25 章。
ActionMailer isn't a real Rails controller, so routes aren't available in your mailer templates. You need to set up instance variables in your mailer class to pass values to the template.
eg. if your mailer is SampleMailer:
Then in the template:
This is explained somewhat cryptically in the api in the "ActionMailer" section, and in more detail in Agile Web Development with Rails, 3rd Ed., Chapter 25.
由于拥有可点击的网址是您在评论中所说的,所以在这里,
我使用了
auto_link
帮助程序在我的一些邮件程序中使网址可点击,效果非常好。我认为电子邮件地址也是如此,请查看完整的 api 以了解所有选项。享受。Since having a clickable url is what you said in your where after in your comment, here you go macek
I've used the
auto_link
helper in some of my mailers to make urls clickable and it has worked out pretty well. I think that does email addresses also, check out the full api for all the options. Enjoy.