Ruby   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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

大佬总结

以上是大佬教程为你收集整理的ruby-on-rails – 在Rails url帮助器中包含受限路由的子域全部内容,希望文章能够帮你解决ruby-on-rails – 在Rails url帮助器中包含受限路由的子域所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: