I have a React client app that needs to talk to a Rails API. I want to use the rails-ujs method Rails.ajax. For example:
Rails.ajax({
type: "POST",
url: "/things",
data: mydata,
success: function(response) {...},
error: function(response) {...}
})
It looks like I can't set data
to a JSON object like this:
mydata = {
thing: {
field1: value1,
field2: value2,
}}
I need to convert it to a application/x-www-form-urlencoded
content type manually like this:
mydata = 'thing[field1]=value1&thing[field2]=value2'
This is ok for flat data but gets complicated quickly for nested data.
jQuery does the conversion automatically before making a request.
So I'm wondering if Rails UJS has some automatic way of doing it, but I couldn't find anything in the docs or code.
Rails.serializeElement
might help: rails-ujs/utils/form.coffee – Daphie