Send POST data without a HTML form
Recently while working on one of the projects I encountered a situation where I had to post some nested-data to a controller method and this data wasn’t associated with any model hence rails fields_for
was of no help.
So to acheive it I created a params json
by fetching the values from DOM and then posted this data by building a form using a JS function as mentioned below.
As we know rails raises ActionController::InvalidAuthenticityToken
error if authenticity_token is missing from params
params["authenticity_token"] = $('meta[name=csrf-token]').attr('content');
in our function takes care of that.