ruby on rails - How to parse param comma separated string ids to has_many mongoid field -
how handle tag_ids in post params save in related model? utilize post_params method.
has_many :tags def post_params params.require(:post).permit(:title, :message, :tag_ids) end #parameters: {"post"=>{"title"=>"asdf", "message"=>"asfd", "tag_ids"=>"543d727a4261729ecd000000,543d8a914261729ecd010000"}} i've got:
mongoid::errors::invalidvalue - problem: value of type string cannot written field of type array i found solution don't it, in post model i've added:
def tag_ids=str str.split(',').each |id| t = tag.find(id) self.tags << t end end
i think have modify incoming info in tag_ids in create action in controller.
so when receive data, before saving info db by, example: post.create! should add together parsing postscontroller action create:
if want array of string:
post.tag_ids = params[tag_ids]split(",") or if want array of integers:
post.tag_ids = params[tag_ids]split(",").map(&:to_i) ruby-on-rails ruby-on-rails-4 mongoid strong-parameters
No comments:
Post a Comment