【发布时间】:2010-06-12 14:29:17
【问题描述】:
有人能描述一下这是怎么回事吗?
在路由文件中:
match "photo", :constraints => {:subdomain => "admin"}
我看不懂。
谢谢
【问题讨论】:
标签: ruby-on-rails
有人能描述一下这是怎么回事吗?
在路由文件中:
match "photo", :constraints => {:subdomain => "admin"}
我看不懂。
谢谢
【问题讨论】:
标签: ruby-on-rails
意思是photo 路由只有在请求包含子域admin 时才会被识别并路由到控制器。例如,Rails 应用程序会响应 http://admin.example.org/photo 的请求,但不会响应 http://example.org/photo。
【讨论】:
match 'photo' => 'photos#show', :constraints => { :subdomain => 'admin' }
我们的一个 posted this today 描述了如何在不同的上下文中重用相同的路由(在这种情况下,无论用户是否登录)
例如,如果您创建一个简单的类来评估真/假:
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)
显然,您可以根据需要设计约束,但 Steve 描述了几个不同的变体。
【讨论】: