[英]TypeError (no implicit conversion of nil into String) ruby on rails 5
So I have a controller that accepts a JSON as payload 所以我有一个接受JSON作为有效载荷的控制器
def create
@tournoi = Tournoi.new(payload: JSON.parse(params[:payload]))
if @tournoi.save
redirect_to root_url
flash[:success] = "Your tournament bracket has been validated!"
the json is from a js diagram that's editable, I am saving the JSON with XMLHttpRquest and formData : json来自可编辑的js图,我使用XMLHttpRquest和formData保存JSON:
function save() {
var tojs = myDiagram.model.toJSON();
var json = JSON.stringify(tojs);
var formData = new FormData();
formData.append("payload", json);
var request = new XMLHttpRequest();
request.open("post", "http://localhost:3000/malesingle", true);
request.send(formData);
}
The JSON is successfully inserted into my controller, I get redirected to a TypeError landing page on my create method from my tournois controller, I know my payload is nill before I save it, I don't know why the way I declared @tournoi = Tournoi.new(payload: JSON.parse(params[:payload]))
isn't guaranteeing the conversion from nill to payload.. JSON已成功插入控制器,我从Tournois控制器重定向到create方法上的TypeError登录页面,在保存之前我知道我的有效载荷
@tournoi = Tournoi.new(payload: JSON.parse(params[:payload]))
,我不知道为什么我声明@tournoi = Tournoi.new(payload: JSON.parse(params[:payload]))
不保证从零到有效负载的转换。
you did a wrong way to submit payload json to rails backend. 您以错误的方式将有效负载json提交到Rails后端。 So params is not json, it's string of json You can try to remove this
所以params不是json,而是json的字符串。您可以尝试删除此字符串
var json = JSON.stringify(tojs);
line from code, and add tojs to formdata directly 从代码行开始,然后将tojs直接添加到formdata
formData.append("payload", tojs);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.