【发布时间】:2010-12-11 19:00:07
【问题描述】:
我有一个来自 Devise 的 url 助手,如下所示:
account_confirmation_url(@resource, :confirmation_token => @resource.confirmation_token)
如何让它使用当前子域而不是主子域创建 url?
【问题讨论】:
标签: ruby-on-rails routing devise
我有一个来自 Devise 的 url 助手,如下所示:
account_confirmation_url(@resource, :confirmation_token => @resource.confirmation_token)
如何让它使用当前子域而不是主子域创建 url?
【问题讨论】:
标签: ruby-on-rails routing devise
Devise wiki 中描述的主要解决方案不适用于设置任意子域,如果您在一个子域(或应用程序的根域)中的请求期间触发生成电子邮件并想要链接,则会出现问题在电子邮件中引用不同的子域。
使其工作的普遍接受的方法是给 url_for 助手一个 :subdomain 选项。
# app/helpers/subdomain_helper.rb
module SubdomainHelper
def with_subdomain(subdomain)
subdomain = (subdomain || "")
subdomain += "." unless subdomain.empty?
host = Rails.application.config.action_mailer.default_url_options[:host]
[subdomain, host].join
end
def url_for(options = nil)
if options.kind_of?(Hash) && options.has_key?(:subdomain)
options[:host] = with_subdomain(options.delete(:subdomain))
end
super
end
end
下一步至关重要,我怀疑这是很多人被绊倒的地方(我知道我犯了)。通过将以下代码添加到 config/application.rb
config.to_prepare do
Devise::Mailer.class_eval do
helper :subdomain
end
end
现在,当您在 Devise 邮件模板中执行 link_to 时,您可以轻松地指定 :subdomain 选项。
link_to 'Click here to finish setting up your account on RightBonus',
confirmation_url(@resource, :confirmation_token => @resource.confirmation_token, :subdomain => @resource.subdomain)
【讨论】:
尝试传递它:host => 'yoursub.domain.com'
【讨论】:
【讨论】: