简体   繁体   English

rails 5.2如何在控制器中获取表单数据值

[英]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. 我有一个简单的HTML表单,我通过使用Fetch API和FormData将一些数据发送到我的服务器。

I'm sending role and user_id with their values to the server. 我正在将roleuser_id及其值发送到服务器。 In controller when I print params I get: 在我打印params控制器中,我得到:

{"-----------------------------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"} {“----------------------------- 1190833731009709688837505639 \\ r \\ nConContent-Disposition:form-data; name”=>“\\”角色\\“\\ r \\ n \\ r \\ nadmin \\ r \\ n ----------------------------- 1190833731009709688837505639 \\ r \\ nContent-处理: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? 如何从中访问并获取roleuser_id值?

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)
    }
);

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. 我通过删除headers: { 'Content-Type':'multipart/form-data' },来解决它headers: { 'Content-Type':'multipart/form-data' },表单获取请求..看起来我们不需要将Content-Type添加到标题中。 anyway my issue solved by this . 无论如何我的问题由此解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在控制器导轨中获取表单数据 - Get form data in controller rails 如何使Trix-Rails在Rails5.2上使用表单工作 - How to get trix-rails work on rails5.2 with a form 如何使用命名空间控制器 rails 5.2 显示数据 - How to display data using a namespaced controller rails 5.2 如何从Ruby上的控制器Rails获取表单字段的值 - How to get the value of form fields from controller Rails on Ruby 在 React 中使用 multipart/form-data 时 Rails Controller 中缺少参数 - Missing parameters in Rails Controller when using multipart/form-data in React Rails:获取具有相同名称的multipart / form-data POST请求参数 - Rails: get multipart/form-data POST request parameters with same name 如何使 Rails 5.2 表单远程提交? - How to make Rails 5.2 form submit remotely? 如何在rails 4 + savon中将pdf文件与SOAP API一起作为多部分/表单数据发布 - how to upload pdf file along with SOAP API as multipart/form-data post in rails 4 + savon Rails:如何通过HTTP将文件作为multipart / form-data发布到Facebook? - Rails: How to post a file via HTTP as multipart/form-data to Facebook? 如何在Rails控制台中获取Controller变量的值 - How to get the value of a Controller variable in rails console
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM