加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 22:19:43 所属栏目:百科 来源:网络整理
导读:说我有以下路由被限制到特定的子域: 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 res
说我有以下路由被限制到特定的子域:
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读