gpt4 book ai didi

ruby-on-rails - rails 4.1 中的子域

转载 作者:数据小太阳 更新时间:2023-10-29 08:08:46 25 4
gpt4 key购买 nike

我正在尝试设置我的 Rails 4.1 应用程序与多个子域一起工作,我看到了 Rails Casts - #221 ,但我有任何问题:

我在生产环境中的域名是:transporte.adm.br *目前还没有什么可展示的!因为它有 2 个域 adm.br,所以我无法完成这项工作。

我的/etc/hosts:

127.0.0.1   localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
127.0.0.1 transporte.adm.br
127.0.0.1 *.transporte.adm.br

路线.rb:

Rails.application.routes.draw do

get '/' => 'login#index', :constraints => { :subdomain => /\bsecure\b/ }

resources :client
constraints(Subdomain) do
get '/' => 'client#index'
end

root :to => 'home#index'
end

url_helper:

 module UrlHelper
def with_subdomain(subdomain)
subdomain = (subdomain || '')
subdomain += '.' unless subdomain.empty?
[subdomain, request.domain(2), request.port_string].join
end

def url_for(options = nil)
if options.kind_of?(Hash) && options.has_key?(:subdomain)
options[:host] = with_subdomain(options.delete(:subdomain))
end
super
end
end

子域名.rb:

class Subdomain
def self.matches?(request)
request.subdomain(2).present? && request.subdomain(2) != 'www'
end
end

我想做的是:当我访问路由时 secure.transporte.adm.br => 'login#index' 以及当我访问任何其他子域路由时 => 'client# index' 没有子域到 => 'home#index'

使用 lvh.me:3000 工作正常,但是当我尝试使用我的域 transporte.adm.br 时却不行!当我使用 transporte.adm.br 访问任何路线时,没有任何反应......

谢谢大家!

最佳答案

您需要做的是告诉 Rails 您的 TLD 的长度。

将此添加到您正确的环境中(config/environments/production.rb 最有可能)

config.action_dispatch.tld_length = 2

然后 request.subdomain 应该会自动为您返回正确的部分

关于ruby-on-rails - rails 4.1 中的子域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24337724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com