ruby-on-rails路由中的约束
有人可以描述一下这是什么吗?
它在路由文件中:
match "photo", :constraints => {:subdomain => "admin"}
我无法理解。
谢谢
它表示如果请求包含子域admin
则只能识别photo
路由并将其路由到控制器。 例如,Rails应用程序将响应http://admin.example.org/photo的请求,但不响应http://example.org/photo 。
我们今天发布的一篇文章介绍了如何在不同的上下文中重用相同的路由(在这种情况下,用户是否登录)
例如,如果您创建一个简单的类来评估true / false:
class LoggedInConstraint < Struct.new(:value) def matches?(request) request.cookies.key?("user_token") == value end end
然后,您可以在路径中使用评估程序来确定适用的路径:
root :to => "static#home", :constraints => LoggedInConstraint.new(false) root :to => "users#show", :constraints => LoggedInConstraint.new(true)
显然你可以根据自己的需要设计约束,但史蒂夫描述了几种不同的变体。