简体   繁体   English

rails 4.0中有多个“root to”路由

[英]Multiple 'root to' routes in rails 4.0

I am trying to get rails to go to different controller#action according to the subdomain, and this is what I have so far in routes.rb 我试图让rails根据子域名转到不同的控制器#动作,这是我到目前为止在routes.rb中所拥有的。

Petworkslabs::Application.routes.draw do

  get '/', to: 'custom#show', constraints: {subdomain: '/.+/'}, as: 'custom_root'
  get '/',  to: "welcome#home", as: 'default_root'
end

rake shows the correct routes I want it to take rake显示了我想要的正确路线

rake routes
      Prefix Verb   URI Pattern             Controller#Action
 custom_root GET    /                       custom#show {:subdomain=>"/.+/"}
default_root GET    /                       welcome#home

But for some reason, I can't get requests like abc.localhost:3000 to hit the custom controller. 但由于某种原因,我无法获得像abc.localhost:3000这样的请求来命中自定义控制器。 It always routes it to welcome#home. 它总是将它路由到欢迎#home。 Any ideas? 有任何想法吗? I am fairly new to rails, so any tips about general debugging would also be appreciated. 我对rails很新,所以关于一般调试的任何提示也将受到赞赏。

EDIT: I stepped through the code using the debugger and this is what I found 编辑:我使用调试器逐步完成代码,这是我发现的

(rdb:32) request.domain "abc.localhost" (rdb:32) request.subdomain "" (rdb:32) request.subdomain.present? (rdb:32)request.domain“abc.localhost”(rdb:32)request.subdomain“”(rdb:32)request.subdomain.present? false

Looks like for some reason rails thinks that the subdomain is not present, even though its there. 看起来由于某种原因,rails认为子域不存在,即使它在那里。 I wonder if its because I am doing this localhost. 我想知道是不是因为我正在做这个本地主机。

Updated Answer: 更新答案:

Worked for me on Rails 3 & 4: 在Rails 3和4上为我工作:

get '/' => 'custom#show', :constraints => { :subdomain => /.+/ }
root :to => "welcome#home"

For some reason request.subdomain was not getting populated at all (I suspect this is because I have doing this on localhost, I have opened a bug here https://github.com/rails/rails/issues/12438 ). 由于某种原因,request.subdomain根本没有填充(我怀疑这是因为我在localhost上做了这个,我在这里打开了一个bug https://github.com/rails/rails/issues/12438 )。 This was causing the regex match in routes.rb to fail. 这导致routes.rb中的正则表达式匹配失败。 I ended up creating a custom matches? 我最终创建了自定义匹配? method for Subdomain which looks something like this Subdomain的方法看起来像这样

class Subdomain
  def self.matches?(request)

    request.domain.split('.').size>1 && request.subdomain != "www"
  end
end

and hooking that up in routes.rb 并在routes.rb中挂钩

constraints(Subdomain) do
  get '/',  to: "custom#home", as: 'custom_root'
end

this seems to work. 这似乎有效。

EDIT: More information in the github issues page https://github.com/rails/rails/issues/12438 编辑:更多信息在github问题页面https://github.com/rails/rails/issues/12438

@manishie's answer is right, but you'll still likely have issues in your devo environment if you're using localhost . @manishie的答案是对的,但是如果您使用的是localhost那么您的devo环境中仍然可能存在问题。 To fix it add the following line to config/environments/development.rb : 要修复它,请将以下行添加到config/environments/development.rb

config.action_dispatch.tld_length = 0

and then use @manishie's answer in routes.rb : 然后在routes.rb使用@manishie的答案:

get '/' => 'custom#show', :constraints => { :subdomain => /.+/ }
root :to => "welcome#home"

The issue is that tld_length defaults to 1 and there's no domain extension when you're using localhost so rails fails to pickup the subdomain. 问题是tld_length默认为1,并且当您使用localhost时没有域扩展,因此rails无法获取子域。 pixeltrix explains it really well here: https://github.com/rails/rails/issues/12438 pixeltrix在这里解释得非常好: https//github.com/rails/rails/issues/12438

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM