ruby-on-rails-3 – Yaml输出填充!map:ActiveSupport :: HashW
发布时间:2020-12-17 03:00:49 所属栏目:百科 来源:网络整理
导读:我升级到rails 3.0.9并且有一个新的yaml gem.不幸的是,to_yaml输出相当大的哈希的方式已经发生了某种变化.现在,我得到了很多“!map:ActiveSupport :: HashWithIndifferentAccess”之前我没有得到的元素,据我所知,这是一个bug,因为我试图使我的.yml尽可能易
我升级到rails 3.0.9并且有一个新的yaml gem.不幸的是,to_yaml输出相当大的哈希的方式已经发生了某种变化.现在,我得到了很多“!map:ActiveSupport :: HashWithIndifferentAccess”之前我没有得到的元素,据我所知,这是一个bug,因为我试图使我的.yml尽可能易读.有没有办法摆脱yaml输出中的“!map:ActiveSupport :: HashWithIndifferentAccess”?它不会影响它加载到我的代码中的方式,所以我没有看到在输出中有它的点.我可以把它搞定,但我认为这里还有其他一些我不知道的事情.
典型的哈希输入: {"All" => {"A test" => {"string"=>"This is just a test","description" => "This is a description for a test string","alternatives" => [{"new" => "woot"}]}} Yaml输出: All: A test: !map:ActiveSupport::HashWithIndifferentAccess string: This is just a test description: This is a description for a test string alternatives: - !map:ActiveSupport::HashWithIndifferentAccess new: woot 我想要的(以及我以前得到的): All: A test: string: This is just a test description: This is a description for a test string alternatives: - new: woot 注意:我的输出必须是UTF-8. 解决方法
我怀疑你的哈希来自控制器中的params,其类型为ActiveSupport :: HashWithIndifferentAccess.修复输出的一种方法是通过猴子修补其to_yaml方法,在将其转换为YAML之前将其自身转换为普通哈希.
# config/initializers/hash_with_indifferent_access.rb class HashWithIndifferentAccess < Hash def to_yaml(opts = {}) self.to_hash.to_yaml(opts) end end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- c – 在BOOST中禁用例外?
- ios – 如何在objc_weak_error上设置中断
- c# – XMLWorker异常:未将对象引用设置为对象的实例
- ruby – 使用bundler时,在gemspec中声明开发依赖项仍然有用
- 如何在 Vue.js 中使用第三方js库
- ruby-on-rails – 用于rails中子域的多个robots.txt
- React-Native 学习系列课程笔记(React-Native 开发从零开始
- cocos2dx 学习笔记
- ruby-on-rails – “$bin / rails”中的“bin”是什么意思?
- ruby-on-rails – 谷歌地图,Ruby on Rails,缩放级别,带一个