如何从整个 Rails 应用程序中删除区分大小写?

发布于 2024-09-09 01:37:14 字数 364 浏览 0 评论 0原文

我有一个基于 Twitter API 构建的相当复杂的 Rails 应用程序,并且希望使整个应用程序不区分大小写。

到目前为止,我们一直尽可能将所有用户名和其他字符串转换为 .downcase 并使用默认的区分大小写的搜索,但这开始导致问题,“用户名”和“用户名”被视为不同的用户。

我是否需要设置此验证:

validates_uniqueness_of :username, :case_sensitive => false

我的每个模型(有很多)上的某处为 并从应用程序中删除 .downcase 的所有实例(有很多)?还有其他想法吗?

注意:这个应用程序还没有上线,所以如果有必要的话,我不介意擦除它现在存储的所有数据。

I have a fairly complex Rails app built on top of Twitter's API, and want to make the whole app case-insensitive.

Up until now, we've been converting all usernames and other strings to .downcase whenever possible and using the default case-sensitive searches, but that's starting to cause problems, with 'Username' and 'username' being considered different users.

Do I need to set this validation:

validates_uniqueness_of :username, :case_sensitive => false

somewhere on every one of my models (there are a lot) and remove all instances of .downcase from the app (there are a TON)? Any other ideas?

Note: this app isn't live yet, so I don't mind wiping all the data it's storing right now, if necessary.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幸福丶如此 2024-09-16 01:37:14

我使用机架中间件解决了这个问题,请参阅此页面: http://gehling.dk/2010/02/how-to-make-rails-routing-case-insensitive/

更新:此代码已更新为可以与 Rails 3 一起使用,所以仍然是一个非常好的选项。

I solved it using Rack Middleware, see this page: http://gehling.dk/2010/02/how-to-make-rails-routing-case-insensitive/

UPDATE: This code has been updated to work with Rails 3 as well, so still a very good option.

蒲公英的约定 2024-09-16 01:37:14

也许在创建选项上设置一个过滤器,在验证之前将所有数据转换为小写?然后,所有数据将被假定为小写。否则,只需编写一个自定义验证器。

Perhaps setting a filter on the create option that converts all the data to downcase BEFORE validation? Then, all the data would be assumed to be in downcase. Otherwise, just write a custom validator.

仅此而已 2024-09-16 01:37:14

您可以构建一个包含常见验证(例如用户名验证)的 mixin,添加不区分大小写,然后将其混合到所有模型中。

You could build a mixin that had your common validations like that username validation, add the case insensitivity, and just mix that in to all your models.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文