1

I'm fairly new to Rails but I have a situation where a user can create a vacancy and select modules for that vacancy by dragging en dropping modules. Everytime something has changed (a module has been added/removes to the vacancy of the order has changed) I send a javascript object to the rails controller through AJAX and I want to extract the values from this object and story them in my DB.

My object will look like this:

addedModules = {
   module: [
      {module_id: '1', name: 'first-module', width: '3', height: '1', position: '1'},
      {module_id: '5', name: 'fifth-module', width: '1', height: '1', position: '2'},
      {module_id: '3', name: 'third-module', width: '4', height: '1', position: '3'},
   ]
};

In my controller I would like to go through every module and extract their module_id, name, etc.

AJAX block:

$.ajax({
    type: "POST",
    url: "../../../vacancies/" + <%= params[:vacancy_id] %> + "/update_with_modules",
    data: "addedModules=" + addedModules
}).done(function (response) {
    console.log(response);
});

Is there a way to do so or is there a better solution?

8
  • Do you find your object in the params? Commented Jan 23, 2017 at 14:04
  • I get the object using params[:addedModules], but if I use params[:addedModules][0] or something like that I get "[" and with params[:addedModules][1] I get "o" so it's getting the word "object" and not the values of the object. Commented Jan 23, 2017 at 14:07
  • try params[:addedModules][:module][0] Commented Jan 23, 2017 at 14:08
  • I get an Internal Server Error back from AJAX Commented Jan 23, 2017 at 14:11
  • Can you show me the logs of this error? Commented Jan 23, 2017 at 14:13

1 Answer 1

1

Change this line:

data: "addedModules=" + addedModules

by this line:

data: {addedModules: addedModules}
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.