mač*_*ček 3 development-environment routes ruby-on-rails actionmailer
我试图在我的用户欢迎电子邮件中提供确认链接,我收到以下Rails错误:
Need controller and action!
Run Code Online (Sandbox Code Playgroud)
它对这一行大惊小怪:
<p>Please take a moment to activate your account by going to:
<%= link_to confirm_user_url(:id => @user.confirmation_code) %>.</p>
Run Code Online (Sandbox Code Playgroud)
在我的development.rb环境中,我有以下几行:
config.action_mailer.default_url_options = {
:host => "localhost", :port => 3000
}
Run Code Online (Sandbox Code Playgroud)
@user变量没有问题.我测试过的电子邮件之类的东西@user.username和@user.confirmation_code.我只是遇到麻烦url_for和命名路线,如confirm_user_url.
当我检查我的路线时rake routes,confirm_user显示,所以这不是指定路线不存在的问题.
我似乎无法弄明白.是什么赋予了?
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
Run Code Online (Sandbox Code Playgroud)
然后在模板中:
<%= link_to "Confirmation link", @confirm_user_url %>
Run Code Online (Sandbox Code Playgroud)
这在"ActionMailer"部分的api中有点隐秘地解释,并且在使用Rails的Agile Web Development,第3版中更详细地解释.,第25章.