开发者

Reusing Rails' param parsing to turn a one dimensional hash into a multideminonal one based on the key values and array notation

I am currently using rturk which give me back my answers in a one dimensional hash as such...

{"answ开发者_开发技巧ers[125][rating]"=>"5", "answers[126][rating]"=>"5", "commit"=>"Take Survey", "answers[125][rating]"=>"5", "authenticity_token"=>"je0Hx48qKmCzy1zmXCpijYWbl4w92eDMRajWJcVYxe0=", "gender"=>"m", "answers[120][rating]"=>"5", "answers[121][rating]"=>"5", "income"=>"$75,000 to $100,000", "answers[122][rating]"=>"5", "date[year]"=>"1992", "career"=>"Marketer", "answers[123][rating]"=>"5", "answers[124][rating]"=>"5"}

What I would like to do is parse those into a multidimensional hash that I can then just pass as attributes. If I can figure this out I'd probably switch from form_tag back to rails stand object forms.


Yes, Rack::Utils.parse_nested_query could help you, but you need to convert hash to string:

h = {"answers[125][rating]"=>"5", ...} # your hash
s = h.to_a.collect { |a| a.join('=') }.join('&')
Rack::Utils.parse_nested_query(s)


Take a look at the Turkee gem (http://github.com/aantix/turkee) as it will do this object mapping for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜