case INsensitive urls?

I've mapped my users' usernames off my root path to give them vanity urls, (i.e. example.com/username), but if you capitalize any of the letters of their username, rails throws an exception. It's looking for the case sensitive version of that name. So for instance, example.com/username works, but example.com/USErNamE doesn't.

I fixed the problem by putting this in my applications_controller.rb:

  rescue_from NoMethodError do |exception|      if request.url != request.url.downcase         redirect_to request.url.downcase      else         flash[:error] = "That doesn't seem to exist."         redirect_to root_path      end   end

All it does is downcases the url, then tries that new url. I actually found that solution on an answer to a stack overflow question that was asking about case insensitive urls. Side note, stackoverflow doesn't seem to be case sensitive, like stackoverflow.com/QueStioNS still displays the proper page and does no downcasing. However, I don't think they are built on rails, but twitter accomplishes this, too. Like, twitter.com/eV still routes to the correct page with no downcasing hack. It actually displays the upcased url but still routes to the correct path. How are they doing this?

Or, how are you solving this?

In the action that handles user's username's requests, why wouldn't you simply downcase the parameter you are passing to your User model's find method?

Ie...

map.user ':user', :controller => 'foo', :action => 'bar'

# in your Foo controller...

def bar    User.find_by_username(params[:user].downcase)    #.... end

Wouldn't that work?

I guess my thinking was that I only wanted to catch potential errors.

Yes your way definitely works and it's probably a bit easier on rails, so thank you.

But wouldn't that method also limit all usernames to their downcased versions? Like a user could never have an example.com/FirstnameLast url could they?

Also, scribed is accomplishing this correctly. It displays any variation of upcased/downcased letters in the url address, but still displays the user's profile correctly. Is this a rails feature, or hack, or some kind of Apache or database feature?

Phillip thank you again for your input.

I guess my thinking was that I only wanted to catch potential errors.

Yes your way definitely works and it's probably a bit easier on rails, so thank you.

But wouldn't that method also limit all usernames to their downcased versions? Like a user could never have an example.com/FirstnameLast url could they?

Yep, but you can't have it both ways. If you have users JoeBob and JOEbob and the browser requests "JoEbOb", which record do you return? You have to pick one and half the time you'll pick wrong.

What you could do is require uniqueness on username's regardless of caps, but let them use caps to make username's easier to read... then you wouldn't have the conflict.

If it were me, I'd force them all lower case and be done with it :slight_smile:

Also, scribed is accomplishing this correctly. It displays any variation of upcased/downcased letters in the url address, but still displays the user's profile correctly. Is this a rails feature, or hack, or some kind of Apache or database feature?

No idea.

Yep, but you can't have it both ways. If you have users JoeBob and
JOEbob and the browser requests "JoEbOb", which record do you return?
You have to pick one and half the time you'll pick wrong.

What you could do is require uniqueness on username's regardless of
caps, but let them use caps to make username's easier to read... then
you wouldn't have the conflict.

That's what I was doing validates_uniqueness_of :username, :case_sensitive => false which prevents JoeBob and JOEbob from signing up. And that might be the way it has to stay,

But, this got me thinking: User.find_by_username(params[:user].downcase) This will find the correct person in the db and return no errors, no matter how many capitalizations someone uses in the URL. But this only works if the original user signed up with no capitalizations in his/her name.

So, if the user signed up as joebob, then JOEbob, JoEbOb, or any other variation will return the correct result, AND it will leave the URL just as the user entered it, caps and all.

But, this doesn't work if the user originally signed up with caps in his name. So, what if I were to create an index in the table for the usernames and pass in the option downcase. I think it would work perfectly then, but I'm not sure if this can be done?

GoodGets,

The way I solved this in my app was to have two columns for the username. One is called "username," which stores the username the way the user wants it to be displayed (with capitalization), and one called "username_key," which is the username converted to lowercase. In my User model, I have this method:

  def username=(value)     write_attribute :username, value     write_attribute :username_key, (value ? value.downcase : nil)   end

With a unique index on "username_key," I'm sure that the database will enforce unique usernames without case sensitivity and I can preserve the capitalization that the user prefers. I use User.find_by_username_key(username.downcase) to find users.

-David

Thanks David. That's almost exactly what I did. However, I used username_down instead of _key. Don't know why you needed to know that, but... you're welcome.

Anyway, after looking into this even more, I found that some databases are case insensitive by default. So instead of this being a rails "problem," I think it's more of a database issue since some databases will find the correct result no matter the casing. I think it all depends on which database you use (postgresql, mysql, etc) and how they're encoded.

Either way, if the database is case Insensitive, well I at least now know the fix. Thanks again David.

No problem. I'm happy to help. I noticed the same thing about some databases being case insensitive, but I don't know enough about the different database systems to be confident relying on that fact myself.

-David