never_had_a_name
never_had_a_name

Reputation: 93216

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"}

I can't understand it.

thanks

Upvotes: 1

Views: 1584

Answers (2)

Zach Moazeni
Zach Moazeni

Reputation: 375

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.

Upvotes: 1

John Topley
John Topley

Reputation: 115372

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.

Upvotes: 3

Related Questions