Why is :required => true not working on collection_select? - ruby-on-rails

I want to make sure the user selects a category in my form before they can submit it, but :required => true doesn't seem to be working. Here's the select:
<%= f.collection_select :category_id, Category.all, :id, :name, :prompt => 'Choose a category' %>
Any advice?

Try this
<%= f.collection_select(:category_id, Category.all, :id, :name, {:prompt => 'Choose a category'}, {:required => true}) %>
Explanation:
According to the Rails documentation the syntax for the collection_select function looks like this:
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
As per the syntax options and html_options are hashes, so you need to enclose them in braces.
Reference - http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select

Related

Rails 5.1 how to not list nulls in a collection_select?

I have a collection_select field listed here:
<%= form.collection_select :parent_id, Document.order(:name), :id, :name,
{:include_blank => ''}, {:class => 'form-control'} %>
The name field has nulls in it for certain cases. I only want to list the name in the collection_select if it is not null.
Is there a way to do this?
Set up a scope in your Document class:
##document.rb
scope :named_documents, -> { where.not(name: nil).order(:name) }
Then you can use it like this:
<%= form.collection_select :parent_id, Document.named_documents, :id, :name, {:include_blank => ''}, {:class => 'form-control'} %>
you can do
<%= form.collection_select :parent_id, Document.named_documents.reject{|d| d.name.nil?}.order(:name), :id, :name,
{:include_blank => ''}, {:class => 'form-control'} %>
I updated it.

Add class to bootstrap_form_for collection_select in Rails 5

I use bootstrap_form_for to create forms and have a collection select, where I want to add a custom class. I tried this, but this does not work:
<%= f.collection_select :location, Location.all, :id, :name, label: 'Location', :include_blank => ("Select..."), hide_label: true, :class => 'location' %>
Any ideas?
Many Rails helpers take multiple hash arguments.
And the definition of this collection_select method looks like this:
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
So your select field will be:
<%= f.collection_select :location, Location.all, :id, :name,
{label: 'Location', :include_blank => ("Select..."), hide_label: true}, {class: "location"} %>

Required Field on collection_select type field

I have a form with a simple text field where I require the form to be filled out:
<%= f.text_field :email, :required => true %>
The next field is a collection_select type where I want to force the user to select a choice. I tried:
<%= f.collection_select(:list_ids, List.where(user_id: current_user), :id, :name, {}, {multiple: true}), :required => true %>
which gives me the error:
syntax error, unexpected ',', expecting ')' ..., :name, {}, {multiple: true}), :required => true );#output_... ... ^
Without the :required => true option the code works fine. How do I force a selection by the user in this case? Thanks
Try changing this
<%= f.collection_select(:list_ids, List.where(user_id: current_user), :id, :name, {}, {multiple: true}), :required => true %>
to this
<%= f.collection_select :list_ids, List.where(user_id: current_user), :id, :name, {}, {multiple: true, required: true} %>
Try this
<%= f.collection_select(:list_ids, List.where(user_id: current_user), :id, :name, {}, {multiple: true, required: true}) %>
Explanation:
According to the Rails documentation the syntax for the collection_select function looks like this:
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
As per the syntax options and html_options are hashes, so you need to enclose them in braces.
Reference - http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select
Credit
<%= form.collection_select :msr_cd,
#msrs, :msr_cd, :msr_cd,
{multiple: false, required: true},
data: { placeholder: "Select Measure" },
class: "form-control col-sm-12 chosen-select"
%>
Note:
#msrs from the controller
:msr_cd - option value`enter code here`
:msr_cd - option text
We can pass the chosen select like above

Rails: Multiple dropdown menus collection_select

Super Rails n00b here: Currently I have a form with the following code:
<%= f.collection_select :account_ids, #accounts, :id, :name, include_blank: true %>
and it currently works how I want it to but now I would like to have multiple drop down menus so I can select multiple accounts. I do not want the multiple select to be on the same dropdown.
If I do this:
<%= f.collection_select :account_ids, #accounts, :id, :name, include_blank: true %>
<%= f.collection_select :account_ids, #accounts, :id, :name, include_blank: true %>
<%= f.collection_select :account_ids, #accounts, :id, :name, include_blank: true %>
only the last selection appears in the params. How can I make it so the params would look like this:
"journal"=>{"account_ids"=>["1","2","3"]}
Can collection.select do this or should I be using something different? Any help would be greatly appreciated. Thanks!
You need to add one option :multiple :
<%= f.collection_select :account_ids, #accounts,
:id, :name, { include_blank: true },
{ multiple: true } %>
Note: :multiple- If set to true the selection will allow multiple choices.
I wrote a little snippet to test it. My code :
<%= form_for #track, url: fetch_path do |f| %>
<%= f.collection_select :label, #tracks, :id, :title, {include_blank: true}, {multiple: true} %>
<% end %>
Here is the page :
Or, if you really want to duplicate:
<% klass = f.object.class.model_name.param_key %>
<%= f.collection_select :account_ids, #accounts, :id, :name, { include_blank: true } , { name: "#{klass}[account_ids][]" } %>
Write the above line 3 times.
If your parameter name ends in "[]", then all inputs with that name will be collated into an array with that name.
So, your select tag (in html) will be like
<select name="account_ids[]"><option>...
and to make this using the collection_select helper, try
<%= f.collection_select :account_ids, #accounts, :id, :name, {include_blank: true}, {:name => 'account_ids[]'} %>

Rails 3: class not recognized for form collection?

I have a nested form with the following field:
<%= f.label :size %><br />
<%= f.collection_select :size, Video::SIZE, :to_s, :to_s, :include_blank => true, :class => "sizefield" %>
As you can see, I've assigned the class, "sizefield", to this field, but for some reason, my app is not recognizing it. What am I doing wrong here?
Rails functions that allow two terminal hashes get confused if you don't specify the boundaries of those hashes exactly. For example:
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
You're not separating the options from the html_options. Try this:
<%= f.collection_select :size, Video::SIZE, :to_s, :to_s, {:include_blank => true}, {:class => "sizefield"} %>

Resources