我很難搞清楚「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