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"} %>
Related
I want a collection select tag in rails to submit two attributes of the chosen item to the controller when the form is submitted. Basically, I have a list of counties and I want to submit both the county and the state as parameters. No problem having it submit one or the other, but not both. Am I thinking about this the wrong way? Here's what I have so far...
<%= form_tag(plans_path, method: 'get', action: 'screen2') do %>
<%= text_field_tag :ZIP, "ZIP Code", id: "zipBlur"%>
<%= collection_select(nil, :county, #counties.order('RES_RATIO DESC'), :COUNTY, :COUNTY_NAME, {:selected => "#{params[:county]}"}) %>
<%= submit_tag 'Screen', :name=> nil %>
<% end %>
Thanks for your help!
using :multiple => true
ex:
<%= collection_select(:ingredient, :supplier_ids,
Supplier.all(:order=>"name ASC"),
:id, :name, {:selected => #ingredient.supplier_ids, :include_blank => true}, {:multiple => true}) %>
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.
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"} %>
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
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