ruby-on-rails – 在Rails url帮助器中包含受限路由的子域

我有以下路由被限制到特定的子域:
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

我的问题是,root_url和backend_root_url都会返回当前子域名的URL:“http://current-subdomain.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

也许我需要手动覆盖网址助手?我如何处理(访问路线等)?

编辑:我可以使用返回“http://admin.lvh.me/”的root_url(:subdomain =>“admin”)获取正确的结果,而不管当前的子域名.但是,我宁愿不必在代码中指定这一点.

解决方法

使用如下所示的“认值”将使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

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...