質問

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.

役に立ちましたか?

解決

What specifically is giving you trouble?

If you have

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"}

you can do things like

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

and so on...

他のヒント

If tyler's method won't work for this one off, and you trust your import, and acknowledge that this is not a good idea... eval will work.

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"]
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top