I have been working on something using the gojs library, which is a library that ables you to generate diagrams, from tournament brackets to family trees etc..
I'm using a save function to submit my set of data to my model via formData,
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);
}
My controller as a set of action following the parsing of payload into params. def create
@tournoi = Tournoi.new(payload: JSON.parse(params[:payload]))
if @tournoi.save
redirect_to malesingle_url
I even used serialize to avoid any trouble while passing the object into tthe model :
class Tournoi < ApplicationRecord
serialize :payload, JSON
end
my model is set-up in a way to accept a json object into the data as 'payload', thanks to postgrel on rails.
class CreateTournois < ActiveRecord::Migration[5.1]
def change
create_table :tournois do |t|
t.json 'payload'
t.timestamps
end
end
end
After saviing the tournoi, the following action is suppose to be a redirection to another view if @tournoi.save redirect_to malesingle_url
The json object is passed throu the parameters as payload it is inserted into tournois table then committed into the data-base.
i get a TypeError (no implicit conversion of nil into String): app/controllers/tournois_controller.rb:7:in create
after clicking on save button, my browser show me an action controller exception with a ** no implicit conversion of nil into string ** highlighting @tournoi = Tournoi.new(payload: JSON.parse(params[:payload]))
while inspecting my page i console on my javascript console a warning Use of Mutation Events is deprecated. Use MutationObserver instead. i went online to look a bit around to understand why im getting this message they all mention dom modification, which what i'm tryiing to do by assigning a json object to data, what i dont uderstand is on my terminal it shows that my object is saved but i get an error. just doesnt really make any sense to me .
request.send(JSON.stringify(tojs))or if it doesn't map automaticallyrequest.send({payload: JSON.stringify(tojs)})request.send(JSON.stringify(tojs))and a syntaxerror onrequest.send({payload: JSON.stringify(tojs)})and nothing inserted into my db