简体   繁体   English

在Rails 5上的TypeError(没有将nil隐式转换为String)ruby

[英]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.

相关问题 类型错误:没有将 nil 隐式转换为 String Heroku 和 Rails - TypeError: no implicit conversion of nil into String Heroku and Rails Rails 6.0.0 TypeError - 没有将 nil 隐式转换为字符串: - Rails 6.0.0 TypeError - no implicit conversion of nil into String: 没有将 nil 隐式转换为 String Ruby on Rails - no implicit conversion of nil into String Ruby on Rails Ruby on Rails:TypeError(没有将字符串隐式转换为整数) - Ruby on Rails: TypeError (No implicit conversion of String into Integer) TypeError(没有将 nil 隐式转换为 String): - TypeError (no implicit conversion of nil into String): rails db schema sql格式TypeError:没有将nil隐式转换为String - rails db schema sql format TypeError: no implicit conversion of nil into String Rails,mongoid抛出TypeError不会将nil隐式转换为String - Rails, mongoid throws TypeError no implicit conversion of nil into String Rails:没有将nil隐式转换为String - Rails: no implicit conversion of nil into String Rails-升级到ruby 2.2.2-没有将数组隐式转换为String(TypeError) - Rails- upgrading to ruby 2.2.2 - no implicit conversion of Array into String (TypeError) TypeError-在Ruby on Rails数组中没有将String隐式转换为Integer - TypeError - no implicit conversion of String into Integer in array Ruby on Rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM