Bav*_*sjo 11 routes ruby-on-rails
假设我有以下限制在特定子域的路由:
App::Application.routes.draw do
constraints :subdomain => "admin" do
scope :module => "backend", :as => "backend" do
resources :signups
root :to => "signups#index"
end
end
constraints :subdomain => "www" do
resources :main
root :to => "main#landing"
end
end
Run Code Online (Sandbox Code Playgroud)
我的问题是root_url和backend_root_url两个返回对当前子域名的URL:"HTTP:// 当前的子域 .lvh.me /"而不是特定的资源子域.我想root_url返回"http:// www .lvh.me /"并backend_root_url返回"http:// admin .lvh.me /"(子域下所有资源的行为应该相同).
我试图在rails 3.2中通过在各个地方设置url选项来实现这一点,一个是应用程序控制器中的url_options:
class ApplicationController < ActionController::Base
def url_options
{host: "lvh.me", only_path: false}.merge(super)
end
end
Run Code Online (Sandbox Code Playgroud)
也许我需要手动覆盖url助手?我将如何处理(访问路线等)?
编辑:我能够使用root_url(:subdomain =>"admin")获得正确的结果,无论当前的子域如何,都返回"http:// admin .lvh.me /".但是,我宁愿不必在代码中指定这一点.
Bav*_*sjo 10
使用如下所示的"defaults"将使rails url helpers输出正确的子域.
App::Application.routes.draw do
constraints :subdomain => "admin" do
scope :module => "backend", :as => "backend" do
defaults :subdomain => "admin" do
resources :signups
root :to => "signups#index", :subdomain => "admin"
end
end
end
constraints :subdomain => "www" do
defaults :subdomain => "www" do
resources :main
root :to => "main#landing"
end
end
end
Run Code Online (Sandbox Code Playgroud)