11

我想让所有满足特定约束的请求都转到特定的控制器。所以我需要一条包罗万象的路线。我如何在 Rails 中指定它?是这样的吗?

match '*', to: 'subdomain_controller#show', constraints: {subdomain: /.+\.users/}

那真的会抓住所有可能的路线吗?即使有许多嵌套目录,也不要漏掉,这一点很重要。

使用 Ruby on Rails 3.2,但准备升级到 4.0。

更新'*path'似乎工作。但是,我遇到的问题是只要文件存在于我的public目录中,Rails 就会呈现它。

4

2 回答 2

18

我认为您需要对这种方法进行细微调整,但您明白了:

更新:

#RAILS 3
#make this your last route.
match '*unmatched_route', :to => 'application#raise_not_found!'

#RAILS 4, needs a different syntax in the routes.rb. It does not accept Match anymore.
#make this your last route.
get '*unmatched_route', :to => 'application#raise_not_found!'

class ApplicationController < ActionController::Base

...
#called by last route matching unmatched routes.  
#Raises RoutingError which will be rescued from in the same way as other exceptions.
def raise_not_found!
    raise ActionController::RoutingError.new("No route matches #{params[:unmatched_route]}")
end
...

end

更多信息在这里: https ://gist.github.com/Sujimichi/2349565

于 2013-10-14T21:10:37.560 回答
0

这应该工作

Calamas::Application.routes.draw do
  get '*path', to: 'subdomain_controller#show',constraints: lambda { |request| request.path =~ /.+\.users/ }
end
于 2013-10-14T20:39:36.657 回答