Rails Ransack include blank in multiple select - ruby-on-rails

I'm using the gem Ransack in a Rails 3.2 app.
The following works to select multiple values:
<td><%= f.collection_select :costproject_coststatus_id_in, Coststatus.all, :id, :statuscode, {}, { :multiple => true}
But, is there a way to include a blank?
I've tried these:
<td><%= f.collection_select :costproject_coststatus_id_in, Coststatus.all, :id, :statuscode, {}, { :multiple => true, :include_blank => true }%></td>
<td><%= f.collection_select :costproject_coststatus_id_in, Coststatus.all, :id, :statuscode, include_blank: true, {}, { :multiple => true }%></td>
Thanks for the help!!

OK - this worked:
<td><%= f.collection_select :costproject_coststatus_id_in, Coststatus.all, :id, :statuscode, {:include_blank => true}, { :multiple => true }%></td>

Related

In a Rails form, how to add a multiselect dropdown?

I have a Rails form which includes a dropdown as shown below:
<%= f.select :facility_id, options_for_select(#entities.collect{ |e| [e.entity, e.entity] }, 1 ), {include_blank: '-Select-'}, { id: my_entity'} %>
I also have 2 more dropdowns similar to the one above.
I would like to make the above dropdown as a multiselect dropdown. How can I achieve that?
Please help!
Thanks
You may just need to add this to the tag
{ :multiple => true}
like this
<%= f.select :facility_id, options_for_select(#entities.collect{ |e| [e.entity, e.entity] }, 1 ), {prompt: '-Select-', :multiple => true}, { id: my_entity'} %>
You can do it by adding multiple: true in select
Example:
<%= f.select :facility_id, options_for_select(#entities.collect{ |e| [e.entity, e.entity] }, 1 ), {include_blank: '-Select-'}, { id: my_entity'}, { :multiple => true, :size => 5 } %>
Reference: [1][2]

How to add required true on collection_select type field

I have already tried to add required=> true and :prompt=>"select..." on the collection_select field, but every time I get syntax error. How to solve this error which is below.
.field
= f.label "Receiver"
= f.collection_select(:receiver_admin_id,
Admin.agent_and_admin(current_admin.id, current_admin.parent_master_agent_id),
:id,
:agent_name,
:prompt => 'Select receiver',
{multiple: true}),
:required => true
= f.collection_select(:receiver_admin_id,
Admin.agent_and_admin(current_admin.id, current_admin.parent_master_agent_id),
:id,
:agent_name,
:prompt => 'Select receiver',
{multiple: true, required: true})
= f.collection_select(:receiver_admin_id,
Admin.agent_and_admin(current_admin.id, current_admin.parent_master_agent_id),
:id,
:agent_name,
:prompt => 'Select receiver',
{},
{multiple: true}),
:required => true
= f.collection_select :receiver_admin_id,
Admin.agent_and_admin(current_admin.id, current_admin.parent_master_agent_id),
:id,
:agent_name,
:prompt => 'Select receiver',
{multiple: true},
:required => true
Try this please:
= f.collection_select :receiver_admin_id, Admin.agent_and_admin(current_admin.id, current_admin.parent_master_agent_id), :id, :agent_name, {prompt: 'Select receiver'}, {multiple: true, required: true}
Your were close almost everytime :) From the docs in the rails guide you have:
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
Using the form helpers (f.) mean that the object (first argument) is taken care of so you're left with:
method, collection, value_method, text_method, options, html_options
Both the multiple and required need to be in the html_options so:
= f.collection_select(:receiver_admin_id, Admin.agent_and_admin(current_admin.id, current_admin.parent_master_agent_id), :id, :agent_name, { :prompt => 'Select receiver' }, multiple: true, :required => true)

Rails undefined method `map' for nil:NilClass on collection_select - The reference called may be nil

After reading through the other SO answers, it's pretty clear that there are some common themes.
Mostly this type of error seems to happen when the object called is not defined yet, but in this case we have a has_many relationship that may not have a referenced entry when building the selection.
Class Tag
property :name, type: String
has_many :in, :tagged, type: :CONCEPTUAL_TAG, model_class: :Artefact
end
One option is to do something like this:
<div class="field">
<%= f.label :tagged_id %><br>
<% if !#tag.tagged.empty? %>
<%= f.collection_select(:tagged, Artefact.all.sort { |a,b| a.name <=> b.name }, :id, :name, options = {:prompt => "Please Select an Item", :selected => #tag.tagged.map(&:id)}, html_options = {:multiple => true, :class=>"search"}) %>
<% else %>
<%= f.collection_select(:tagged, Artefact.all.sort { |a,b| a.name <=> b.name }, :id, :name, options = {:prompt => "Please Select an Item"}, html_options = {:multiple => true, :class=>"search"}) %>
<% end %>
</div>
But this is definitely not DRY.
Is there a way to select nothing when there is no association, and pre-populate when there is while keeping to a single f.collection_select?
You don't have to use the condition in your view. Even if there are no items in #tag.tagged, it is still an ActiveRecord::Relation instance, so you can go ahead and just call map on it:
<%= f.collection_select(:tagged,
Artefact.all.sort { |a,b| a.name <=> b.name },
:id,
:name,
options = {
:prompt => "Please Select an Item",
:selected => #tag.tagged.map(&:id)
},
html_options = {
:multiple => true,
:class=>"search"
})
%>

rails form select multiple gives empty first value

I have defined has_and_belongs_to_many associations between Meals and Recipes. In the Meals create form, I am using a select to populate the recipes.
<%= f.select :recipes, Recipe.all.collect { |x| [x.name, x.id]}, {}, :multiple => true %>
But the result set has a nil as the first value.
"recipes"=>["", "2", "7"]
How can I eliminate the empty/nil value?
For me setting :include_hidden => false is what worked
<%= f.select :recipes, Recipe.all.collect { |x| [x.name, x.id]}, {:include_hidden => false}, :multiple => true %>
You can reject blank option by passing :include_blank => false
<%= f.select :recipes, Recipe.all.collect { |x| [x.name, x.id]}, {:include_blank => false}, :multiple => true %>
And you can set an prompt as following
<%= f.select :recipes, Recipe.all.collect { |x| [x.name, x.id]}, {:include_blank => "Please Select"}, :multiple => true %>

Rails select tag with required true

I have the following select tag
<%= f.select :id, User.find(:all, :conditions => ["manager = ?", false]).collect {|u| [u.username, u.id]}, {:required => true}, {:class => "multiselect", :multiple => true} %>
I try to add :required => true to it, the view renders but :required => true doesn't work!.
Quite a bit late, but for anyone looking, checking at the reference, should go explicitly as the hash, so ruby interpreter doesn't get confused:
select(object, method, choices = nil, options = {}, html_options = {}, &block)
For this special case:
<%= f.select :id,
User.find(:all, :conditions => ["manager = ?", false]).collect {|u| [u.username, u.id]},
{:prompt => 'Select something'},
{:required => true, :class => "multiselect", :multiple => true} %>
Try this:
<%= f.select :id, User.find(:all, :conditions => ["manager = ?", false]).collect {|u| [u.username, u.id]}, {:multiple => true}, :class => "multiselect", :required => true %>

Resources