ActionMailer 视图中的 url_for 和命名路由出现问题:“需要控制器和操作”

发布于 2024-08-29 14:13:03 字数 800 浏览 2 评论 0原文

我试图在我的用户欢迎电子邮件中提供确认链接,但收到以下 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 技术交流群。

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

发布评论

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

评论(2

夜血缘 2024-09-05 14:13:03

ActionMailer 不是真正的 Rails 控制器,因此路由在邮件程序模板中不可用。您需要在邮件程序类中设置实例变量以将值传递给模板。

例如。如果您的邮件程序是 SampleMailer:

class SampleMailer < ActionMailer::Base

  def confirmation_mail(user)
    subject    'Confirmation'
    recipients user.email
    from       '[email protected]'
    sent_on    Time.now
    body       :greeting => 'Sample Greeting', :email => user.email,
               :confirm_user_url => confirm_user_url(:id=>@user.confirmation_code)
  end
end

则在模板中:

<%= link_to "Confirmation link", @confirm_user_url %>

这在“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:

class SampleMailer < ActionMailer::Base

  def confirmation_mail(user)
    subject    'Confirmation'
    recipients user.email
    from       '[email protected]'
    sent_on    Time.now
    body       :greeting => 'Sample Greeting', :email => user.email,
               :confirm_user_url => confirm_user_url(:id=>@user.confirmation_code)
  end
end

Then in the template:

<%= link_to "Confirmation link", @confirm_user_url %>

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.

仅此而已 2024-09-05 14:13:03

由于拥有可点击的网址是您在评论中所说的,所以在这里,

<p>Please take a moment to activate your account by going to: 
<%= auto_link confirm_user_url(:id => @user.confirmation_code) %>.</p>

我使用了 auto_link 帮助程序在我的一些邮件程序中使网址可点击,效果非常好。我认为电子邮件地址也是如此,请查看完整的 api 以了解所有选项。享受。

Since having a clickable url is what you said in your where after in your comment, here you go macek

<p>Please take a moment to activate your account by going to: 
<%= auto_link confirm_user_url(:id => @user.confirmation_code) %>.</p>

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.

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