I'm trying to adapt Editor REST interface to my Rails app and I'm stuck in passing params to Rails 5, so it can do create
. By default Editor is passing params like this:
Processing by Strongbolt::UserGroupsController#create as JSON
Parameters: {"data"=>{"0"=>{"name"=>"testing", "description"=>"test",
"users"=>{"0"=>{"id"=>"3"}, "1"=>{"id"=>"2"}, "2"=>{"id"=>"5"}},
"users-many-count"=>"3", "roles"=>{"0"=>{"id"=>"1"}}, "roles-many-count"=>"1"}}}
However I need them to be passed like this in order to Rails 5 to do Create action:
Processing by Strongbolt::UserGroupsController#create as JSON
Parameters: {"strongbolt_user_group"=>{"name"=>"this is my test group",
"description"=>"this is my test description", "user_ids"=>["2", "5"], "role_ids"=>["1"]}}
In my JS the Editor part looks like this, where in Create action I've hard-coded example values:
editor = new $.fn.dataTable.Editor( {
ajax: {
create: {
type: 'POST',
url: '/strongbolt/user_groups',
data: {"strongbolt_user_group":{
"name": "this is my test group",
"description": "this is my test description",
"user_ids": [2,5],
"role_ids": [1]}
}
},
edit: {
type: 'PATCH',
url: '/user_groups/', //Not final implementation
},
remove: {
type: 'DELETE',
url: '/strongbolt/user_group'
}
},
table: "#user_groups_table",
template: '#user_groups_form',
display: "details",
idSrc: "id",
fields: [ {
name: "name"
}, {
name: "description"
}, {
type: "checkbox",
name: "users[].id",
optionsPair: {
label: 'name',
value: 'id'
}
}, {
type: "checkbox",
name: "roles[].id",
optionsPair: {
label: 'name',
value: 'id'
}
}
]
} );
I believe I should somehow define variables and then insert them into data
in my Create action, but I don't know how to do it. My idea of variables is like this:
var fname = editor.field( 'name' );
var fdescription = editor.field( 'description' );
var fusers = editor.field( 'users[].id' );
var froles = editor.field( 'roles[].id' );
How do I implement those variables to pass so Rails can do Create action, please?