我试图在我的用户欢迎电子邮件中提供一个确认链接,但我收到以下 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 routes
检查我的路线时, confirm_user
出现,所以这不是命名路由不存在的问题。我似乎无法弄清楚。是什么赋予了?
最佳答案
ActionMailer 不是真正的 Rails Controller ,因此您的邮件程序模板中没有可用的路由。您需要在邮件程序类中设置实例变量以将值传递给模板。
例如。如果您的邮寄者是 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 %>
这在 api 中有一些隐晦的解释。在“ActionMailer”部分,更详细的信息在 Agile Web Development with Rails, 3rd Ed. ,第 25 章。
关于ruby-on-rails - ActionMailer View : "Need controller and action" 中 url_for 和命名路由的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2687126/