Question

BACKGROUND: Posts have many Communities. On my new post form, I have a multiple select box for selecting communities using Select2.

When selecting a community without Select2, I get these params, and everything works ok:

...{"community_ids"=>["","1"]},...

When selecting a community with Select2 I get:

...{"community_ids"=>["[],1"]},...

which throws a Couldn't find Community with id=0 error. I've narrowed it down to having something to do with how strong parameters works. I'm guessing Rails is trying to find a community with and id of "[]"? This is in my PostsController:

def post_params
  params[:post].permit(:post_field1, :post_field2, { :community_ids => [] })
end

Any ideas?

Was it helpful?

Solution

What rails is doing is taking the input ("[],1") and calling to_i on it (to integer). So you get

"[],1".to_i #=> 0

You would somehow need to split ["[],1"] into ["[]","1"].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top