开发者

How do I handle uppercase and lowercase characters in a custom url?

I want to let users have links to their profiles using their registered usernames.

I store their username exactly how they give it.

I set up my routes to match /:name and then used find_by_name to get it. The problem I have is when you type in example.com/username it doesn't work the name: Username. (Note the开发者_开发知识库 uppercase/lowercase difference)

So my question is how can I ignore case in urls?


You can store the username downcased along with a display_name which is in the format they gave it to you. Then, downcase any input and match it with username and only use display_name for, well, display :)

If you wanted, you could even redirect to /DisPlAyName from /username after you look up the record.


Easiest way is to convert the username in the database and in rails to lower (or upper) case when you are doing the comparison.

User.where('lower(username) = ?', params[:name].downcase).first

Or if you are still using rails 2:

User.find(:first, :conditions => ['lower(username) = ?', params[:name].downcase])


One way to do this is to store a lowercase version of their username. When they type in

example.com/UsErNaMe or example.com/Username

downcase it and match it to the lowercase version in the database.

---OR---

Just make the user have a lowercase username everywhere. (Downcase it when its made)

---OR---

Have a constant type of username. So always have the first letter capitalized or something. Store it as lower case and upcase the first letter everytime you show it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜