微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/ruby/266200.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐