1
    $.ajax({
        type: 'POST',
        url: '/users',
        data: {
            _method : 'PUT',
            user : {
                guides : {
                    step1 : true,
                    step2 : true
                }
            }
        }
    });

Is this saving correctly? I want this json data in a rails serialized field but It's saving incorrectly as follows below which is causing errors.

User.guided:

--- "{\"step1\"=>\"true\", \"step2\"=>\"true\"}"

Then when I do the following in the rails view:

guides = [<%= current_user.guides.try(:html_safe)%>];

It outputs with => instead of the expected :.

halfer
  • 19,824
  • 17
  • 99
  • 186
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012

1 Answers1

0

First of all you could try to use JSON.stringify() otherwise jQuery will use $.param() to serialize your data. But your main issue is that you want a JSON string, and not the YAML that is generated. As far as I now something like

guides = [<%= current_user.guides.to_json %>];

should do the trick. Also, maybe I'm not 100% sure but you probably don't need to use html_safe on this, because it's already escaped, although can't tell how it will be rendered in view

Leszek Zalewski
  • 416
  • 4
  • 5