Array returns first element blank in my Rails(3.2.11) multi-select - ruby-on-rails

When I selected multiple values from select list then array returns the first value empty.
= f.select :assignedto, options_from_collection_for_select(User.all, 'name', 'name',f.object.assignedto),{}, { :multiple => true}
I tried with {:include_blank => false} and {:include_hidden => false} but this is not working for rails 3.2.11. I have many solutions to handle this empty value in the controller but I want to stop adding empty value in the array.

Properly because the second arg is 'name' instead of 'id'.
Try
options_from_collection_for_select(User.all, 'id', 'name', f.object.assignedto)

Related

How can remove Null value from f.select in rails?

I have multiple dropdown when I select items. It also creates null value How can I remove null values from this array?
= f.select(:doc, file.all.collect {|a| [a.name, a.id]}, {}, id: "id-select2", class: "form-control", :multiple => true).
You have two problems there
Your collect is returning some nil elements(As Joseph said), in this case the name attribute is what it can be nil, so you can check for that on the collect
Solution(compact) [UPDATE]
f.select(:doc, file.all.collect {|a| [a.name, a.id] if a.name, include_hidden: false }.compact, {}, id: "id-select2", class: "form-control", :multiple => true)
Specify the include_blank option https://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-select
f.select(:doc, file.all.collect {|a| [a.name, a.id] if a.name }.compact, { include_blank: false, include_hidden: false }, id: "id-select2", class: "form-control", :multiple => true)
According to the docs for select helper I have found this gotcha
The HTML specification says when multiple parameter passed to select
and all options got deselected web browsers do not send any value to
server. Unfortunately this introduces a gotcha: if an User model has
many roles and have role_ids accessor, and in the form that edits
roles of the user the user deselects all roles from role_ids multiple
select box, no role_ids parameter is sent. So, any mass-assignment
idiom like To prevent this the helper generates an auxiliary hidden
field before every multiple select. The hidden field has the same name
as multiple select and blank value.
Note: The client either sends only the hidden field (representing the
deselected multiple select box), or both fields. This means that the
resulting array always contains a blank string.
In case if you don't want the helper to generate this hidden field you
can specify include_hidden: false option.
So if you add the include_hidden: false option then you won't get the empty string on your multiple option when the data is sent to the controller.
You can use compact. For example:
a = [nil, 2, 3, 4, 5]
without_nil = a.compact
# [2, 3, 4, 5]
using compact! will modify the original array whereas compact returns a new array.
you can filter that in database query for example (I assume id is primary key and is not null):
file.where('name IS NOT NULL').load
compact isn't working for you because you're likely calling it on the result of the collect. If you have an array like [['a', 1], ['b', nil]], calling compact on it won't do anything because the ['b', nil] is not nil. It only contains it. So you need to avoid loading the Files where name == nil.
You'd want something like this instead:
f.select(:doc, file.where('name IS NOT NULL').collect { |f| [f.name, f.id] }, {}, id: "id-select2", class: "form-control", multiple: true)
It might be more helpful if we knew exactly what file is.

Set :selected value based on variableX -or- the user submitted value if variableX is nil

Have this select dropdown input field that is populated with state names.
I want to set the selected value to be the content of #state.id if this is not nil. If there is already a chosen option then I would like that to be selected instead.
How can this be done correctly? I tried several solution all of which fails, latest code example im using now:
= p.input :state,
:collection => ["State1","State2"]
:selected => (#state.id.to_s rescue nil) || (params[:state] rescue nil),
:id => "state",
:name => "state",
:prompt => t('forms.choose')
The problem is that the :selected option works based on two values.
The index of the element in the collection array, or
The value itself
So lets say you want the the last option to be selected, in your case you would need:
:selected => 1
or
:selected => "State2"
I hope it helps.

Rails Multiple Select boxes: Injecting default values from params

I currently have a multiple select box in a rails form that looks like this:
= select_tag :in_all_tags, options_from_collection_for_select(Tag.where(:project_id => #project.id), :id, :name, #in_all_tags_param), { :id => "tags", :tabindex => "3", "data-placeholder" => "Choose Tags", :multiple => "multiple" }
Where
#in_all_tags_param = params[:in_all_tags]
The problem is, #in_all_tags_param will only populate the select form with the last value from params[:in_all_tags]. So, if the url string reads in_all_tags=5&in_all_tags=8, the pre-selected value in the multiple select will only be 8.
From what I understand, the way around this is to append [] to the field name for multiple params, so that :in_all_tags becomes in_all_tags[]
BUT, when I try this, submitting the form returns:
Expected type :default in params[:in_all_tags], got Array
Any suggestions appreciated.
Cheers...
You need to add a :name element to the same hash with :multiple => true in it. So I use something similar for Genres on an app for mine and I do { :multiple => true, :name => "lesson[genre_ids][]" }. The name has to be model[attribute][].

ruby on rails select collection, filter results from array [enumerable]

current i am trying to restrict the information feed into an option select field to only display the criteria i have selected. with the code below this seems to be working
= select("schedule", :selected_players, User.where(:team_id => current_user[:team_id]) { |p| [full_name(p), p.id] }, {:include_blank => 'None', :prompt => 'Add Players to Lineup'}, :multiple => "multiple")
the issue is that this code is display an array field type i.e #<User:0xa559830>
how do i get it to display the actual users name?
Define .to_s method in model
Like here
https://github.com/roolo/mwstt/blob/master/app/models/project.rb#L7
Also all the mapping and searching logic should be placed in model as method which you'll just call in view, or prepare it in controller!

:prompt appearing in collection_select when a records value is expected

Sorry, this may seem like a simple issue, but: I have a collection_select element that is called via ajax from a _updateregions.html.erb file for creating and editing records that looks like:
<%= collection_select(:wine, :wineregionid, regions, :wineregionid, :regionname,
options = {:selected => :wineregionid, :prompt => "Select a Region"}
) %>
The problem is, when editing an existing record, the prompt is appearing by default instead on the records value. When I remove the :prompt, it works fine... question is, how can I make this work for both the New and Edit case?
According to http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html I think I'm doing it right....
collection_select(object, method,
collection, value_method, text_method,
options = {}, html_options = {})
Returns and tags for
the collection of existing return
values of method for object‘s class.
The value returned from calling method
on the instance object will be
selected. If calling method returns
nil, no selection is made without
including :prompt or :include_blank in
the options hash.
i think :prompt donot takes a string .
it should be true/false or null.
try this
<%= collection_select(:wine, :wineregionid, regions, :wineregionid, :regionname,
options = {:selected => :wineregionid, :prompt => true) %>

Resources