开发者

constraints in ruby-on-rails routing

Could someone describe what this is all about?

It's in the routing file:

match "photo", :constraints => {:subdomain => "admin"}开发者_JS百科

I can't understand it.

thanks


It's saying that the photo route will only be recognised and routed to a controller if the request contains the subdomain admin. For example, the Rails application would respond to a request of http://admin.example.org/photo, but not http://example.org/photo.


One our guys posted this today which describes how you could reuse the same routes with different contexts (in this case whether the user is logged in)

For instance if you create a simple class to evaluate true/false:

class LoggedInConstraint < Struct.new(:value)
  def matches?(request)
    request.cookies.key?("user_token") == value
  end
end

You can then use the evaluator in the routes to determine what routes apply:

root :to => "static#home", :constraints => LoggedInConstraint.new(false)
root :to => "users#show", :constraints => LoggedInConstraint.new(true)

Obviously you can design the constraints to your needs, but Steve described a couple different variants.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜