1

So I have a web app and for it, I store raw params in the database in case I ever need to track down an issue and unfortunately I had an issue where it would be useful to go back to the original form parameters to sort our some missing RSVPs to an event.

Unfortunately the form has a few nested fields and parsing those back out has proven to be tough. Here is an example of a raw form post (with the data obfuscated):

{"utf8"=>"✓", "authenticity_token"=>"XXXX...=", "focus_group_invite_url_code"=>"XXXXXXX", "event_group_id"=>{"-1"=>""}, "event"=>{"id"=>"-1"}, "spouse"=>{"name"=>""}, "email"=>{"address"=>"XXXX@XXXX.com"}, "phone"=>{"number"=>"5555551212"}, "commit"=>"RSVP", "action"=>"create", "controller"=>"my_controller"}

I've tried using a few ways to split into hashes, but because of the nested nature of a few of these this is proving tough. I also tried using URI.www_form_decode and ActiveSupport::JSON.decode and neither worked. I'm thinking there must be an easy way to do this that I'm missing. Hoping someone has a suggestion.

4

2 回答 2

2

具体是什么给你带来了麻烦?

如果你有

data = {"utf8"=>"✓", "authenticity_token"=>"XXXX...=", "focus_group_invite_url_code"=>"XXXXXXX", "event_group_id"=>{"-1"=>""}, "event"=>{"id"=>"-1"}, "spouse"=>{"name"=>""}, "email"=>{"address"=>"XXXX@XXXX.com"}, "phone"=>{"number"=>"5555551212"}, "commit"=>"RSVP", "action"=>"create", "controller"=>"my_controller"}

你可以做类似的事情

data["authenticity_token"] #=> "XXXX...="
data["event"]["id"] #=> "-1"
data["phone"]["number"].to_i #=> 5555551212

等等...

于 2013-07-25T21:47:00.957 回答
2

如果 tyler 的方法不适用于此方法,并且您信任您的导入,并承认这不是一个好主意... eval 将起作用。

1.9.3p327 > s = "{'one' => 1, 'two' => 2}"
 => "{'one' => 1, 'two' => 2}"
1.9.3p327 > h = eval(s)
 => {"one"=>1, "two"=>2}
1.9.3p327 > h.keys
 => ["one", "two"]
于 2013-07-25T21:48:48.417 回答