简体   繁体   English

不区分大小写的用户名URL

[英]Username URLs that aren't case sensitive

I have an application where users set usernames, and their profile URL is matched to their username similar to Twitter. 我有一个应用程序,用户在其中设置用户名,其个人资料URL与类似于Twitter的用户名相匹配。 The issue I'm running into is case sensitivity. 我遇到的问题是区分大小写。 I'm down casing usernames before they save to the database, but I'm still getting the issue. 在将用户名保存到数据库之前,我不使用大小写的用户名,但是仍然遇到问题。 Here's how I'm matching the URL's in routes 这是我在路线中匹配网址的方式

  match "/:id" => "users#show", via: :get

Assuming that you store it in the column called username . 假设您将其存储在名为username的列中。

In you Users Controller show action. 在您的用户控制器中show操作。 You need to down case both the username column and the username from the params. 您需要小写用户名列和参数中的用户名。

So you need to change: 因此,您需要更改:

@user = User.find_by_username(params[:id])

To be: 成为:

@user = User.find(:first, :conditions => ["lower(username) = ?", params[:id].downcase])

For a cleaner approach you may change params[:id] to be params[:username] to avoid confusion. 对于更清洁的方法,您可以将params[:id]更改为params[:username]以避免混淆。

Your route will be: 您的路线将是:

match "/:username" => "users#show", via: :get

Finding the user will be using params[:username] 查找用户将使用params[:username]

@user = User.find(:first, :conditions => ["lower(username) = ?", params[:username].downcase])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM