如何从整个 Rails 应用程序中删除区分大小写?
我有一个基于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我使用机架中间件解决了这个问题,请参阅此页面: 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.
也许在创建选项上设置一个过滤器,在验证之前将所有数据转换为小写?然后,所有数据将被假定为小写。否则,只需编写一个自定义验证器。
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.
您可以构建一个包含常见验证(例如用户名验证)的 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.