当出现问题时,Rails回应了给定的params,它看起来像这样:
参数:
{"utf8"=>"✓",
"filter"=>{"title"=>"1",
"address"=>"2",
"city"=>{"title"=>"3"},
"people"=>{"name"=>"4",
"surname"=>"5",
"patronymic"=>"6",
"emails"=>{"email"=>"7"}},
"emails"=>{"email"=>"8"}},
"sort"=>{"0"=>{"field"=>"",
"dir"=>"asc"},
"1"=>{"field"=>"",
"dir"=>"asc"}},
"commit"=>"Show"}
但如果看起来像这样,它实际上会更有用:
{
"utf8" => "✓",
"filter" => {
"title" => "1",
"address" => "2",
"city" => {
"title" => "3"
},
"people" => {
"name" => "4",
"surname" => "5",
"patronymic" => "6",
"emails" => {
"email" => "7"
}
},
"emails" => {
"email" => "8"
}
},
"sort" => {
"0" => {
"field" => "",
"dir" => "asc"
},
"1" => {
"field" => "",
"dir" => "asc"
}
},
"commit" => "Show"
}
如何实现?
答案 0 :(得分:0)
使用vanilla Ruby,最好的方法是将哈希值转换为JSON:
JSON::pretty_generate(hash_here, :allow_nan => true, :max_nesting => false)
这会输出对象的美化JSON版本(数组,哈希或其他)。它不会是纯粹的红宝石,但它来自基础库,所以不需要安装任何东西。