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

ruby-on-rails – rails 4.0中的多个“root to”路由

发布时间:2020-12-17 02:46:13 所属栏目:百科 来源:网络整理
导读:我试图让rails根据子域名转到不同的控制器#动作,这是我到目前为止在routes.rb中所拥有的. Petworkslabs::Application.routes.draw do get '/',to: 'custom#show',constraints: {subdomain: '/.+/'},as: 'custom_root' get '/',to: "welcome#home",as: 'defau
我试图让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显示了我想要的正确路线

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

但由于某种原因,我无法获得像abc.localhost:3000这样的请求来命中自定义控制器.它总是将它路由到欢迎#home.有任何想法吗?我对rails很新,所以关于一般调试的任何提示也将受到赞赏.

编辑:我使用调试器逐步完成代码,这是我发现的

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

看起来由于某种原因,rails认为子域不存在,即使它在那里.我想知道是不是因为我正在做这个本地主机.

解决方法

更新答案:

在Rails 3&amp ;;上为我工作4:

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

(编辑:李大同)

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

    推荐文章
      热点阅读