Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

So I have this weird problem with jquery doing a ajax post with a ruby object. I have 2 options. 1. $('form_id'}.serialize(). I know this works. But my form simply has too many fields, and it is a little too inconvenient to manage them in the form 2. Second option, the rails controller is constructing a ruby object along the way as the form is being edited. This is super convenient only if I can simply stick in the object in the ajax post request. Apparently I am able to, with something like this...

some_function() {
  $.ajax( {type: post, url: '/action', data: { 'post_data': <%= @ruby_object.to_json %> }});
}

If you wonder this works, it does. (dunno how and why) But there is a small problem. the ruby object has a array of hashes within a hash, like this { [ {} ] } It appears that someone somewhere is converting that array to a hash with keys which are indices of that array. So in the action, if i print the parameters, I get { { {} } }. So can someone tell me how not to get this and get back exactly the original object structure?

share|improve this question
    
can you post some of your Ruby code that is creating "ruby_object"? –  Matthew J Morrison Jul 1 '10 at 19:17
    
What ruby web framework are you using? –  Adrian Jul 1 '10 at 19:56
    
{ [ {} ] } is not possible, hashes are key/value pairs, you can't just drop an array in the middle of it. –  Joshua Cheek Jul 2 '10 at 3:23
    
can you give us a more concrete example? ie what does the hash/array/hash look like with actual data (instead of when empty)? –  Taryn East May 16 '11 at 11:25

1 Answer 1

You do $.toJSON on your JS object, and post it as 'json' format. And if you're curious, how Rails converts the post data to Ruby object, check actionpack-3.0.7/lib/action_dispatch/middleware/params_parser.rb

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.