Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails 5.2 how to get form-data value in controller

I have a simple HTML form and I'm sending some data to my server by using Fetch API and FormData.

I'm sending role and user_id with their values to the server. In controller when I print params I get:

{"-----------------------------1190833731009709688837505639\r\nContent-Disposition: form-data; name"=>"\"role\"\r\n\r\nadmin\r\n-----------------------------1190833731009709688837505639\r\nContent-Disposition: form-data; name=\"user_id\"\r\n\r\n1\r\n-----------------------------1190833731009709688837505639--\r\n", "controller"=>"users", "action"=>"updaterole", "id"=>"1"}

How can I access and get role and user_id value from this?

This is my script on client side:

var form = document.querySelector("#roleForm");
var formdata = new FormData(form);

fetch(url, {
    method: "PATCH",
    headers: { 'Content-Type':'multipart/form-data' },
    body: formdata,
}).then(
    response => response.text() // .json(), etc.
    // same as function(response) {return response.text();}
).then(
    html => {
        console.log(html)
    }
);
like image 698
devmrh Avatar asked Feb 18 '19 18:02

devmrh


1 Answers

in my case, that it is alredy mentioned in comments, we have incorrect body. i solved it by removing headers: { 'Content-Type':'multipart/form-data' }, form fetch request.. looks like we dont need add Content-Type to header. anyway my issue solved by this .

like image 138
devmrh Avatar answered Sep 28 '22 08:09

devmrh