<分区>
Rails 中的参数散列能否将键规范化为不区分大小写?
如果 Rails 收到带有以下查询参数的 GET
请求 http://localhost.com:3000/username?debug=true&vErBoSe=true
params[:verbose]
将为 nil
,您需要使用 params[:vErBoSe]
来获取该值。
<分区>
Rails 中的参数散列能否将键规范化为不区分大小写?
如果 Rails 收到带有以下查询参数的 GET
请求 http://localhost.com:3000/username?debug=true&vErBoSe=true
params[:verbose]
将为 nil
,您需要使用 params[:vErBoSe]
来获取该值。
最佳答案
一种选择是在访问其值之前将参数哈希转换为不区分大小写处理键的哈希。
Ryan McGeary 在 https://stackoverflow.com/a/2030565/454094 上有一个不区分大小写哈希的示例实现,我已在下面复制:
require "active_support/hash_with_indifferent_access"
class CaseInsensitiveHash < HashWithIndifferentAccess
# This method shouldn't need an override, but my tests say otherwise.
def [](key)
super convert_key(key)
end
protected
def convert_key(key)
key.respond_to?(:downcase) ? key.downcase : key
end
end
有了它,您可以:
CaseInsensitiveHash.new(params)[:verbose]
关于ruby-on-rails - rails : Case insensitive params hash keys?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21715364/
相关文章:
ruby-on-rails - ActiveModel::Errors 转换错误消息的属性部分?
sql - 在 PostgreSQL 数据库中定期同步数据的最有效方法是什么?
regex - 在 Ruby 中仅删除字符串中的非前导和非尾随空格?
ruby-on-rails - 如何在 factory_girl 工厂中包含模块?
ruby-on-rails - 将 Rails/ClearDB App 推送到 Heroku 错误 'Can' t 连接到 '127.0.0.1' 上的 MySQL 服务器
ruby-on-rails - OAuthException : This authorization code has been used - Facebook