如何在邮件模板中生成正确的 URL?
我正在使用 Ruby on Rails 3.1.0,我想在 HTML 电子邮件中正确生成 URL。在我的环境文件中,我设置
config.action_mailer.default_url_options = { :host => 'my_site.org' }
在电子邮件视图文件 (.html.erb
) 中,我声明
<%= link_to @user.name, users_url(@user) %>
当我查看收到的电子邮件时,生成的 URL 为 http://users/1
code>,当然不正确。那么,如何在邮件模板中生成正确的 URL,以便在正文消息中包含 http://my_site.org/users/1
链接?
我还尝试设置default_url_options
在我的 mailer.rb
文件中,
class MyCustom::Mailer < ActionMailer::Base
default_url_options[:host] = 'my_site.org'
def test_sending
...
end
end
但它不起作用。
I am using Ruby on Rails 3.1.0 and I would like to properly generate URLs in HTML email messages. In my environment file I set
config.action_mailer.default_url_options = { :host => 'my_site.org' }
In the email view file (.html.erb
) I state
<%= link_to @user.name, users_url(@user) %>
When I go to see the received email the generated URL is http://users/1
, of course no correct. So, how can I generate correct URLs in mailer templates so to have http://my_site.org/users/1
links in body messages?
I also tryed to set the default_url_options
in my mailer.rb
file
class MyCustom::Mailer < ActionMailer::Base
default_url_options[:host] = 'my_site.org'
def test_sending
...
end
end
but it doesn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
users_path
是相对路径 (/users/1)。对于电子邮件,您需要绝对路径,因此请使用users_url(@user)
,这将给出 http:// myapp.com/users/1 相反。users_path
is the relative path (/users/1). For an email, you want the absolute path, so useusers_url(@user)
, which will give http://myapp.com/users/1 instead.您的 action_mailer 设置正确。
但你应该使用 _url 而不是 _path 作为 link_to,
your action_mailer setting is correct.
But you should be using _url and not _path for the link_to,
查看您是否设置了配置选项。为了确保它使用绝对路径,请使用:
或在链接中专门设置主机:
此处解释:
ActionView 助手
See that you set the config option. To be sure it uses the absolute path use:
OR set the host specifically in the link:
It is explained here:
ActionView Helpers