3

我试图在我的用户欢迎电子邮件中提供确认链接,但我收到以下 Rails 错误:

Need controller and action!

它对这一行大惊小怪:

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

在我的development.rb环境中,我有以下行:

config.action_mailer.default_url_options = {
  :host => "localhost", :port => 3000
}

@user变量没有问题。@user.username我已经用和之类的东西测试了电子邮件@user.confirmation_code。我只是遇到麻烦url_for并命名为confirm_user_url.

当我检查我的路线时rake routesconfirm_user会出现,因此命名路线不存在不是问题。

我似乎无法弄清楚。是什么赋予了?

4

2 回答 2

10

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

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

class SampleMailer < ActionMailer::Base

  def confirmation_mail(user)
    subject    'Confirmation'
    recipients user.email
    from       'sample@example.com'
    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中有一些神秘的解释,在Agile Web Development with Rails, 3rd Ed中有更详细的解释。,第 25 章。

于 2010-04-21T23:47:01.643 回答
3

因为有一个可点击的 url 是你在评论之后在 where 中所说的,所以你去 macek

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

我已经auto_link在我的一些邮件中使用了帮助程序来使 url 可点击,并且效果很好。我认为电子邮件地址也是如此,请查看所有选项的完整 api。享受。

于 2010-04-23T04:51:27.717 回答