Rails:与 Hash#to_param 相反 [英] Rails: Opposite of Hash#to_param
本文介绍了Rails:与 Hash#to_param 相反的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我将哈希转换为查询字符串,如何再次将其转换回来?
If I convert a hash to a query string, how can I convert it back again?
{:filters => {:colour => ['Red', 'Blue'], :size => 'Medium'}}.to_param
=> "filters[colour][]=Red&filters[colour][]=Blue&filters[size]=Medium"
Rails 在填充 params
哈希时似乎会自动执行此操作,但是否可以直接调用此方法?
Rails appears to do this automatically when it populates the params
hash, but is it possible to call this method directly?
谢谢.
推荐答案
您正在寻找 Rack::Utils.parse_nested_query(query)
,它将把它转换回 Hash代码>.您可以使用此行获取它:
You're looking for Rack::Utils.parse_nested_query(query)
, which will convert it back into a Hash
. You can get it by using this line:
require 'rack/utils'
这篇关于Rails:与 Hash#to_param 相反的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文