我已经转过来了,使用to_query方法将一个哈希转换为params。
如何将其变回哈希?
我试过这个:
require 'rack'
@tester = Rack::Utils.parse_nested_query(params[:search])
在视图中:<%= @tester.class %>
给出了NilClass。
参数是:
"search"=>"fields%5B%5D=exhb_0&fields%5B%5D=exh0_1&fields%5B%5
D=t_g_a&fields%5B%5D=hp_1&fields%5B%5D=s1&fields%5B%5D=overflade_0&railing%5B%5D
=A-3&railing_m=0&type%5B%5D=ltrappa&wood%5B%5D=wood_6"
答案 0 :(得分:8)
这是this questions的副本:只需使用:
Rack::Utils.parse_query(my_query_string)
为了解码示例中的行,请务必先取消字符串:
require 'rack'
my_string = 'fields%5B%5D=exhb_0&fields%5B%5D=exh0_1&fields%5B%5D=t_g_a&fields%5B%5D=hp_1&fields%5B%5D=s1&fields%5B%5D=overflade_0&railing%5B%5D=A-3&railing_m=0&type%5B%5D=ltrappa&wood%5B%5D=wood_6'
unescaped_string = URI.unescape(my_string)
# => "fields[]=exhb_0&fields[]=exh0_1&fields[]=t_g_a&fields[]=hp_1&fields[]=s1&fields[]=overflade_0&railing[]=A-3&railing_m=0&type[]=ltrappa&wood[]=wood_6"
params_hash = Rack::Utils.parse_query(unescaped_string)
# => {"fields[]"=>["exhb_0", "exh0_1", "t_g_a", "hp_1", "s1", "overflade_0"], "railing[]"=>"A-3", "railing_m"=>"0", "type[]"=>"ltrappa", "wood[]"=>"wood_6"}