collection , :include_blank false not taken in account

503 views
Skip to first unread message

erwin

unread,
Apr 11, 2012, 6:13:15 AM4/11/12
to SimpleForm
I am displaying a collection w multiple select, and I get a wrong list
of selected items...

= f.input :local_contacts, :collection =>
member_options(@project), :label_method => :first, :value_method
=> :last, :selected => @project.local_contacts.map{|local_contact|
local_contact.user[:id] }, :input_html => { :multiple =>
true }, :include_blank => false, :label => t(:local_contact, :count =>
2).capitalize

this generates the following html :

<select name="project[local_contacts][]" multiple="multiple"
id="project_local_contacts" class="select optional">
<option selected="selected" value="1">Mr Jean Bordeaux</option>
<option selected="selected" value="16">Mme User1 Client1</option>
</select>

and I get 3 items as param, first is blank....
params[:project]["local_contacts"] => ["", "1", "16"]

I tried to move the :include_blank => false option into
the :input_html hash, same issue...

looking into the source code I've seen a skip_include_blank? false by
default ... which could explain why a get a blank item , but how to
get rid of it ?

thanks for feedback

erwin

unread,
Apr 11, 2012, 6:29:17 AM4/11/12
to SimpleForm
just for info : tested with simple_form_bootstrap example, same
issue :

new article params
params
{"utf8"=>"✓",
"authenticity_token"=>"E4mnngwuKryEfEzc3O2H78tyiQizhWWY2cMx7dA0fHI=",
"article"=>{"name"=>"", "published"=>"0", "content_type_1"=>[""],
"content_type_3"=>["", "Blog", "Announce"], "category"=>"",
"content"=>""}, "commit"=>"Create Article", "action"=>"create",
"controller"=>"articles"}

we can see : "content_type_3"=>["", "Blog", "Announce"] 3 items
but only 2 selected in the list

Carlos Antonio da Silva

unread,
Apr 11, 2012, 7:22:14 AM4/11/12
to plataformate...@googlegroups.com
The html select you've sent is the only output you get, or you have the entire input with wrapper? I can't see SimpleForm generating anything wrong with that, which makes me think that might be the browser sending an empty value or something.

-- 
At.
Carlos Antonio

Vasiliy Ermolovich

unread,
Apr 11, 2012, 7:31:10 AM4/11/12
to plataformate...@googlegroups.com
When you are using multiple select rails generates hidden input with blank value for you: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select
But I don't see it in your generated html :\

2012/4/11 Carlos Antonio da Silva <carlosanto...@gmail.com>

Carlos Antonio da Silva

unread,
Apr 11, 2012, 7:47:38 AM4/11/12
to plataformate...@googlegroups.com
Ah great Vasiliy, I didn't remember that happens to select + multiple as well, thanks :D

The hidden field might be before the select he posted, so the full html would help to check that.

-- 
At.
Carlos Antonio

erwin

unread,
Apr 11, 2012, 8:00:42 AM4/11/12
to SimpleForm
Thanks Carlos & Vasily
Ok now I see this hidden input w empty value ...
I guess it's a standard Rails output when using multiple .... ( found
similar questions on the web... about it)
but why ? and why it's not possible to get rid of it w an option .. ?

<div class="controls">
<input type="hidden" value="" name="project[local_contacts][]">
<select name="project[local_contacts][]" multiple="multiple"
id="project_local_contacts" class="select optional">
<option selected="selected" value="1">Mr Jean Bordeaux</option>
<option selected="selected" value="16">Mme User1 Client1</option>
</select>
</div>


On Apr 11, 1:47 pm, Carlos Antonio da Silva
<carlosantoniodasi...@gmail.com> wrote:
> Ah great Vasiliy, I didn't remember that happens to select + multiple as well, thanks :D
>
> The hidden field might be before the select he posted, so the full html would help to check that.
>
> --
> At.
> Carlos Antonio
>
>
>
>
>
>
>
> On Wednesday, April 11, 2012 at 8:31 AM, Vasiliy Ermolovich wrote:
> > When you are using multiple select rails generates hidden input with blank value for you:http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelp...
> > But I don't see it in your generated html :\
>
> > 2012/4/11 Carlos Antonio da Silva <carlosantoniodasi...@gmail.com (mailto:carlosantoniodasi...@gmail.com)>
> > > The html select you've sent is the only output you get, or you have the entire input with wrapper? I can't see SimpleForm generating anything wrong with that, which makes me think that might be the browser sending an empty value or something.
>
> > > --
> > > At.
> > > Carlos Antonio
>
> > > On Wednesday, April 11, 2012 at 7:29 AM, erwin wrote:
>
> > > > just for info : tested with simple_form_bootstrap example, same
> > > > issue :
>
> > > > new article params
> > > > params
> > > > {"utf8"=>"✓",
> > > > "authenticity_token"=>"E4mnngwuKryEfEzc3O2H78tyiQizhWWY2cMx7dA0fHI=",
> > > > "article"=>{"name"=>"", "published"=>"0", "content_type_1"=>[""],
> > > > "content_type_3"=>["", "Blog", "Announce"], "category"=>"",
> > > > "content"=>""}, "commit"=>"Create Article", "action"=>"create",
> > > > "controller"=>"articles"}
>
> > > > we can see : "content_type_3"=>["", "Blog", "Announce"] 3 items
> > > > but only 2 selected in the list
>

erwin

unread,
Apr 11, 2012, 8:04:19 AM4/11/12
to SimpleForm
I have seen the 'gotcha' in Rails doc.. so it seems impossible to get
rid of the first null value on the client..
need to eject it from the list upon reception on the server ...

On Apr 11, 1:47 pm, Carlos Antonio da Silva
<carlosantoniodasi...@gmail.com> wrote:
> Ah great Vasiliy, I didn't remember that happens to select + multiple as well, thanks :D
>
> The hidden field might be before the select he posted, so the full html would help to check that.
>
> --
> At.
> Carlos Antonio
>
>
>
>
>
>
>
> On Wednesday, April 11, 2012 at 8:31 AM, Vasiliy Ermolovich wrote:
> > When you are using multiple select rails generates hidden input with blank value for you:http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelp...
> > But I don't see it in your generated html :\
>
> > 2012/4/11 Carlos Antonio da Silva <carlosantoniodasi...@gmail.com (mailto:carlosantoniodasi...@gmail.com)>
> > > The html select you've sent is the only output you get, or you have the entire input with wrapper? I can't see SimpleForm generating anything wrong with that, which makes me think that might be the browser sending an empty value or something.
>
> > > --
> > > At.
> > > Carlos Antonio
>
> > > On Wednesday, April 11, 2012 at 7:29 AM, erwin wrote:
>
> > > > just for info : tested with simple_form_bootstrap example, same
> > > > issue :
>
> > > > new article params
> > > > params
> > > > {"utf8"=>"✓",
> > > > "authenticity_token"=>"E4mnngwuKryEfEzc3O2H78tyiQizhWWY2cMx7dA0fHI=",
> > > > "article"=>{"name"=>"", "published"=>"0", "content_type_1"=>[""],
> > > > "content_type_3"=>["", "Blog", "Announce"], "category"=>"",
> > > > "content"=>""}, "commit"=>"Create Article", "action"=>"create",
> > > > "controller"=>"articles"}
>
> > > > we can see : "content_type_3"=>["", "Blog", "Announce"] 3 items
> > > > but only 2 selected in the list
>

Vasiliy Ermolovich

unread,
Apr 11, 2012, 8:29:25 AM4/11/12
to plataformate...@googlegroups.com
Right now yeah, you can't. BTW, I've made a pull-request with this features some weeks ago https://github.com/rails/rails/pull/5414 :)

2012/4/11 erwin <kado...@gmail.com>

Carlos Antonio da Silva

unread,
Apr 11, 2012, 8:47:16 AM4/11/12
to plataformate...@googlegroups.com
Yeah, by default Rails default collection_ids setter should handle that for you, but if you handle the list manually, that's up to you to do it. Great!

-- 
At.
Carlos Antonio

Reply all
Reply to author
Forward
0 new messages