我很难搞清楚“Rails方式”以向邮件程序添加电子邮件确认URL。Rails:使自定义URL助手的行为像内置_path&_url助手
我选择不纯粹以REST风格执行此操作,因为使用文本电子邮件很困难,因为它们不能PUT
请求。
所以这里是我的routes.rb:get 'confirm/:id' => 'Confirmations#confirm'
,并在我的邮件我喜欢把email_confirm_url(@user.email_token)
,我希望发生的URL。
我创造了一个帮手:
#app/helpers/confirmations_helper.rb
module ConfirmationsHelper
def email_confirm_url(token)
"/confirm/#{token}"
end
end
这一切工作的排序,除非我打电话email_confirm_url(@user.email_token)
...
我真的得到: “/confirm/abcdefg…
”
当我要的是: http://myhostname/confirm/abcdefg…
或正在开发中:http://localhost:3000/confirm/abcdefg…
如何让我的URL帮助器更像在Rails中构建的<resource>_path
和<resource>_url
帮助器?虽然现实,我想我真的只有需要_url
。
#Edit:我有这个在我的环境配置:
#config/environments/development.rb
...
config.action_mailer.default_url_options = { :host => "localhost:3000" }
'ActionView :: Template :: Error:未定义方法'host'for nil:NilClass'我想我记得有些东西在Mailer中无法访问'host'或'host_with_port'!任何方式在这个? – Meltemi
基于上述评论更新了答案,让我知道如果这有效。 – zarazan