1

我无法让我的路由约束在 Rails 4 中工作。这是我的路由文件:

Catapult::Application.routes.draw do
  constraints Subdomain do
    resources :contacts do
      member do
        get 'delete'
      end
    end
  end
end

这是子域约束:

class Subdomain
  def self.matches?(request)
    p request.subdomains.first # This never appears in the logs
    request.subdomains.first !~ /www|catapultcentral|customercube|lvh/
  end
end

如上面的代码中所述,p语句的输出从未出现在日志中,并且似乎从未应用约束。这是的输出rake routes

        Prefix Verb   URI Pattern                    Controller#Action
delete_contact GET    /contacts/:id/delete(.:format) contacts#delete
      contacts GET    /contacts(.:format)            contacts#index
               POST   /contacts(.:format)            contacts#create
   new_contact GET    /contacts/new(.:format)        contacts#new
  edit_contact GET    /contacts/:id/edit(.:format)   contacts#edit
       contact GET    /contacts/:id(.:format)        contacts#show
               PATCH  /contacts/:id(.:format)        contacts#update
               PUT    /contacts/:id(.:format)        contacts#update
               DELETE /contacts/:id(.:format)        contacts#destroy

约束也不会出现在输出中......

请问我做错了什么?

4

1 回答 1

0

我认为您可能需要使用约束类的实例,您必须调用 Subdomain.new:

Catapult::Application.routes.draw do
  constraints Subdomain.new do
    resources :contacts do
      member do
        get 'delete'
      end
    end
  end
end

这意味着它matches?不应该是一个类方法,而是一个实例方法:

class Subdomain
  def matches?(request)
    request.subdomains.first !~ /www|catapultcentral|customercube|lvh/
  end
end
于 2014-06-12T16:41:10.580 回答