1

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 .

2
  • Did either of these work? request.send(JSON.stringify(tojs)) or if it doesn't map automatically request.send({payload: JSON.stringify(tojs)}) Commented Apr 25, 2018 at 14:35
  • @Nope i get a TypeError on request.send(JSON.stringify(tojs)) and a syntaxerror on request.send({payload: JSON.stringify(tojs)}) and nothing inserted into my db Commented Apr 25, 2018 at 15:13

1 Answer 1

0

An error is always raised when using @tournoi = Tournoi.new(payload: JSON.parse(params[:payload])), The TypeError is raised specifically on JSON.parse(params[:payload]). So by just modifying the method create :

def create
      if @tournoi = Tournoi.new(payload: params['payload'])
          @tournoi.save
.......

Not parsing the params will save the payload but it will save another tournoi that has a payload nil

all i need to do is add a validation in my model to avoid a tournoi that has a nil payload to be saved :

class Tournoi < ApplicationRecord
  validates :payload, presence: true, allow_nil: false

Thats it gets the job done :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.