【发布时间】:2017-11-15 22:20:06
【问题描述】:
我正在构建一个具有 merchant 子域的 Rails 应用程序。我有这两条路线:
get '/about', controller: :marketing, action: :about, as: :about
get '/about', controller: :merchant, action: :about, constraints: { subdomain: 'merchant' }, as: :merchant_about
但是当我使用他们的 URL 助手时,merchant_about_url 和 about_url 都会导致 http://example.com/about。
我知道我可以在帮助程序上指定 subdomain 参数以在 URL 前加上子域,但由于这些 URL 将在各种情况下经常使用,我想为这个帮助程序构建一个包装器它更聪明。
我的问题:我可以检查给定路由以查看它是否具有子域约束吗?
如果可以,我想做如下的事情:
def smart_url(route_name, opts={})
if # route_name has subdomain constraint
opts.merge!({ subdomain: 'merchant' })
end
send("#{route_name}_url", opts)
end
这样做,我可以有效地调用:
smart_url('about') # http://example.com/about
smart_url('merchant_about') # http://merchant.example.com/about
这可能吗?
【问题讨论】:
标签: ruby-on-rails routes rails-routing url-helper