简体   繁体   English

从 Rails 控制器中的 Javascript 对象获取值

[英]Get values from Javascript object in Rails controller

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.我对 Rails 还很陌生,但我有一种情况,用户可以通过拖放模块来创建一个空缺并为该空缺选择模块。 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.每次发生变化时(一个模块已添加/删除到订单的空缺已更改)我通过 AJAX 向 rails 控制器发送一个 javascript 对象,我想从这个对象中提取值并将它们记录在我的数据库中。

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.在我的控制器中,我想遍历每个模块并提取它们的 module_id、名称等。

AJAX block: AJAX 块:

$.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?有没有办法这样做或者有更好的解决方案?

Change this line:改变这一行:

data: "addedModules=" + addedModules

by this line:通过这一行:

data: {addedModules: addedModules}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM