Twitter like route

Hi,

I have this route that let me build custom url for users like
/thisismyname, and it works fine. But when I look at the log there is
something I don't like...

When I hit /gregory this is what is happenning:
1. Going to public_profile#public # Good
2. hitting /assets and trying to find a user with asset token # Not good

I thought my constraints would avoid this but it doesn't seem like it...

class PublicProfileConstraint
  def self.matches?(request)
     ['assets', 'admin'].include?(request.session[:token])
  end
end

get "/:token" => "profiles#public", :as => :public_profile,
    :constraints => PublicProfileConstraint

Here is the log:

Started GET "/gregory" for 127.0.0.1 at 2012-03-05 12:44:43 -0800
  Processing by ProfilesController#public as HTML
  Parameters: {"token"=>"gregory"}
  Lawyer Load (0.2ms) SELECT "users".* FROM "lawyers" WHERE
"users"."id" = ? LIMIT 1 [["id", 1]]
  Lawyer Load (0.3ms) SELECT "users".* FROM "lawyers" WHERE
"users"."token" = 'gregorymarcilhacy' LIMIT 1
Rendered profiles/_modal.haml (0.1ms)
....
Rendered profiles/show.haml within layouts/application (154.7ms)
Completed 200 OK in 431ms (Views: 174.0ms | ActiveRecord: 5.5ms)

... Redering js files ...

# I DONT WANT THIS
Started GET "/assets/" for 127.0.0.1 at 2012-03-05 12:44:45 -0800
Served asset - 404 Not Found (10ms)
  Processing by ProfilesController#public as */*
  Parameters: {"token"=>"assets"}
  Lawyer Load (0.2ms) SELECT "lawyers".* FROM "lawyers" WHERE
"users"."id" = ? LIMIT 1 [["id", 1]]
  Lawyer Load (0.3ms) SELECT "lawyers".* FROM "lawyers" WHERE
"users"."token" = 'assets' LIMIT 1
Redirected to http://localhost:3000/
Completed 302 Found in 312ms

... Rendering images ...

# AND I DONT WANT THIS
Started GET "/" for 127.0.0.1 at 2012-03-05 12:44:45 -0800
  Processing by LandingController#landing as */*
  Lawyer Load (0.3ms) SELECT "lawyers".* FROM "users" WHERE
"users"."id" = ? LIMIT 1 [["id", 1]]
Rendered landing/landing.haml within layouts/landing (0.8ms)
Completed 200 OK in 288ms (Views: 23.5ms | ActiveRecord: 2.2ms)

Your constraint is slightly wrong.

Unless you are settings “request.session[:token]” somewhere else in your code, chances are it’s going to be nil. What you most likely want to check for is “request.params[:token]” which will match the token in the URL.

You also have it backwards. The constraint would currently only ALLOW assets and admin. You need to negate that statement.

Fix those 2 issues and you should be in business.