如何在邮件模板中生成正确的 URL?

发布于 2024-12-26 11:53:20 字数 716 浏览 6 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

几度春秋 2025-01-02 11:53:20

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 use users_url(@user), which will give http://myapp.com/users/1 instead.

美人骨 2025-01-02 11:53:20

您的 action_mailer 设置正确。

但你应该使用 _url 而不是 _path 作为 link_to,

<%= link_to @user.name, users_url(@user) %>

your action_mailer setting is correct.

But you should be using _url and not _path for the link_to,

<%= link_to @user.name, users_url(@user) %>
在你怀里撒娇 2025-01-02 11:53:20

查看您是否设置了配置选项。为了确保它使用绝对路径,请使用:

<%= link_to, "My Profile", users_url(:only_path => false, @user) %>

或在链接中专门设置主机:

<%= link_to, "My Profile", users_url(:host => "example.com", @user) %>

此处解释:

ActionView 助手

See that you set the config option. To be sure it uses the absolute path use:

<%= link_to, "My Profile", users_url(:only_path => false, @user) %>

OR set the host specifically in the link:

<%= link_to, "My Profile", users_url(:host => "example.com", @user) %>

It is explained here:

ActionView Helpers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文