如何在Rails 4中做路由约束

时间:2013-04-13 14:15:00

标签: ruby-on-rails routing subdomain ruby-on-rails-4

这不起作用:

get '/' => 'addresses#show', :constraints => Subdomain

LIB / subdomain.rb:

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

这条路线被忽略......

谢谢

2 个答案:

答案 0 :(得分:2)

<强>解决方案:

指定的路线必须先行

root to: 'home#index' 

答案 1 :(得分:0)

只有您需要手动在路线中添加subdomain.rb

,这才能完美运行
get '/' => 'addresses#show', :constraints => Subdomain

LIB / subdomain.rb:

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

添加routes.rb

  require 'subdomain'

是的,指定的路线必须先行

root to: 'home#index'