Reputation: 349
I have a post controller saving posts with different attributes. There are attachments on the post as well. These attachments return just an id for the object. my_cards, my_folders, other_cards and other_folders are all such fields. These are defined as arrays in the model.
I am passing the right params from the form as shown below. All the correct data is present.
PARAMS
{"_id"=>"", "title"=>"Test Post Card attach 28", "description"=>"",
"privacy"=>"public", "url_ids"=>"", "image_ids"=>"",
"attached_card_ids"=>"", "my_card_ids"=>:my_card_ids,
"my_folder_ids"=>"5d21dc7c616b656a9c030000",
"other_card_ids"=>"5d1b1942616b656d10360000",
"other_folder_ids"=>"5cfe2bdf69702d0aa3010200", "domain"=>"localhost",
"controller"=>"api/v1/posts", "action"=>"create",
"owner_id"=>"5d125102616b657131020000",
"user_id"=>"5d125102616b657131020000"}
But when I save the post, the post_params method marks some fields as unpermitted even thought they are defined as arrays in the post_params method.
POST-PARAMS
Unpermitted parameters: _id, url_ids, image_ids, attached_card_ids,
my_card_ids, other_card_ids, other_folder_ids, domain
{"title"=>"Test Post Card attach 28", "privacy"=>"public",
"description"=>"", "owner_id"=>"5d125102616b657131020000",
"user_id"=>"5d125102616b657131020000", "my_folder_ids"=>nil}
My post_params method looks like this
def post_params
params.permit(:id, :title, :privacy, :description,
:owner_id,:user_id,
:url_ids => [], :image_ids => [], :attached_card_ids => [],
my_card_ids: [], my_folder_ids: [],
:other_card_ids => [], :other_folder_ids => [])
end
I have tried multiple ways of defining an array in the post_params method after research on other Stack Overflow questions.
EDIT
I have managed to save the record in the Mongo database. The attributes look like this.
"my_card_ids" : [
"5d1b83a6616b6523a9020000,5d1b9893616b653abd0b0000"
],
"my_folder_ids" : [
"5d21dc7c616b656a9c030000"
],
"other_card_ids" : [
"5d1b168c616b656d10140000"
],
"other_folder_ids" : [
"5cfe2bdf69702d0aa3010200"
]
They are being stored as strings, how can I save them as IDs, same as this
"collections" : [
ObjectId("5d125102616b657131020000"),
]
Upvotes: 0
Views: 113
Reputation: 190
Can you add HTML of the form you're using to post the form data.
The params you've displayed above show
"my_folder_ids"=>"5d21dc7c616b656a9c030000",
"other_card_ids"=>"5d1b1942616b656d10360000",
"other_folder_ids"=>"5cfe2bdf69702d0aa3010200"
as strings. In the controller you have allowed them as arrays while you send them as plain strings.
To fix this, the html name attribute for the fields must be name="my_folder_ids[]"
instead of name=my_folder_id
Upvotes: 3