POST payload to rails model t.json 'payload'

11 views
Skip to first unread message

Loua Max Desire

unread,
Apr 5, 2018, 2:26:25 PM4/5/18
to Ruby on Rails: Talk

So guys i'm working on a tournament bracket based on the GOjs library, The bracket has score input. Once my user is done editing the bracket i save the bracket into a json variable :

function save() {
    var tojs = myDiagram.model.toJSON();
    var payload = JSON.parse(tojs);
    stringify_tojs = JSON.stringify(payload);
    myDiagram.isModified = false;

I use XMLHttpRequest to able to post the payload into my rails model that handles 'payload' :

var request = new XMLHttpRequest();
    request.onload = callback;
    request.open("post", "http://localhost:3000/malesingles");
    request.setRequestHeader("Content-Type", "application/json");
    request.send(payload);

I dont know where i went wrong but i'm certain it's around my controller params but i can't find my mistake already been a week, the controller looks something like this :

@tournoi = Tournoi.new(bracket_params)
  if @tournoi.save
    redirect_to root_url
    flash[:success] = "Your tournament bracket has been validated!"
  #  redirect_to @tournoi
  else
    render 'new'
  end

end i have included the bracket_params in private settings

def bracket_params
      params.require(:tournoi).permit(:payload)
     end

Tried different method to post the payload none really work, would appreciate some help to understand where i went wrong, i get a param is missing or empty :/.

Hassan Schroeder

unread,
Apr 5, 2018, 2:47:02 PM4/5/18
to rubyonrails-talk
On Thu, Apr 5, 2018 at 9:56 AM, Loua Max Desire <lou...@gmail.com> wrote:

> Tried different method to post the payload none really work, would
> appreciate some help to understand where i went wrong, i get a param is
> missing or empty :/.

What is actually arriving as the raw params -- are you inspecting it
via logging statements or pry or ??

Have you tried submitting a properly-shaped payload via e.g. curl
or wget to confirm your controller action is handling it correctly?

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
twitter: @hassan
Consulting Availability : Silicon Valley or remote
Reply all
Reply to author
Forward
0 new messages