0

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?

neon
  • 2,811
  • 6
  • 30
  • 44
  • 1
    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"] – jokklan Jun 21 '13 at 09:23
  • What version of Select2 are you using? Also, can you post your JavaScript code? – georgebrock Jun 26 '13 at 14:53
  • @jokklan you should post that comment as an answer. – pjmorse Jun 27 '13 at 13:57

1 Answers1

0

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"].

jokklan
  • 3,520
  • 17
  • 37