I want to use jQuery validation plugin for select boxes, but I don't know how to add value ="" in select. Here is my code:
<%= f.select :language, options_for_select([
"--- please select ---",
"Arabic",
"Chinese"
])
%>
I want to add
value="" option="please select"
How I can do it in Rails ?
<%= f.select :language, options_for_select([
"Arabic",
"Chinese"
], {:prompt => "please select"})
%>
Related
The answers to how do I use a default placeholder ... in a select_tag? show how to set a default value for the select field.
For example:
<%= f.select :country, countries, {include_blank: true, prompt: "United States"}, {required: true, class: "form-control"} %>
The problem here is that I only want the silhouette of the placeholder, not an actual value that could accidentally be submitted.
If we compare to the placeholder value in a text_field, the difference is much more clear. We see the placeholder:
But we cannot accidentally submit the placeholder.
How can I get similar behaviour with the .select field?
That is, so that there is some placeholder in the select field, but so it cannot be accidentally submitted?
Just change the boolean value of include_blank: true to the desired placeholder string:
<%= f.select :country,
countries,
{ include_blank: "- nothing selected -" },
{ required: true, class: "form-control" }
%>
This will create a pre-selected select option with no value, which will be interpreted by Rails controllers as nil.
According to https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select a select has no placeholder option. If you wish to show a predefined value that should not be submitted you can add a disabled option to this specific item and combine it with selected.
<%= f.select :country, [['USA', 1], ['Canada', 2]], { disabled: '1', selected: '1' } %>
I am new to Rails forms and have the following form fields:
<%= f.label :name, class: "black-text" %>:
<%= f.text_field :name, class: "white task-card", :required => true %>
<%= f.label :foo, class: "black-text" %>:
<% options = [['Option A', 1], ['Option B', 2], ['Option C', 3], ['Option D', 4]] %>
<%= f.select :foo, options_for_select(options), {}, {:include_blank => true, :required => true, class: "white task-card"} %>
If left blank, the text input field will not advance and correctly alerts the user "Please fill out this field," but the select option does not. If the select field is left to its default blank value, the page won't advance, so it works that way, but there is no alert for the user on why nothing is happening. How can I make the browser's standard HTML5 "Please do not fill out this field" alert work for the select field?
Thank you!
You'll need another hash for providing html options, since the first hash, which, for example, contains include_blank option, is for tag options:
<% options = [['Option A', 1], ['Option B', 2], ['Option C', 3], ['Option D', 4]] %>
<%= f.select :foo, options_for_select(options),
{:include_blank => true, class: "white task-card"}, { :required => true } %>
By the way, I don't think include_blank option, since the field is required
I think there is a syntax issue:
<%= f.select :foo, options_for_select(options), {:include_blank =>
true, class: "white task-card"}, { :required => true } %>
Hope this helps
I have select
<%= f.select :visibility, collection_for_visibility_select, :include_blank => false %>
And a helper with values for select:
def collection_for_visibility_select
[
[l(:label_crm_contacts_visibility_project), Contact::VISIBILITY_PROJECT],
[l(:label_crm_contacts_visibility_public), Contact::VISIBILITY_PUBLIC],
[l(:label_crm_contacts_visibility_private), Contact::VISIBILITY_PRIVATE]
]
end
I want to add default select value to the select, and this is what I tried:
<%= f.select :visibility, collection_for_visibility_select, :selected => Contact::VISIBILITY_PUBLIC, :include_blank => false %>
it gave me default select value, but when i want to edit record and switch visibility to something else,i still got VISIBILITY_PUBLIC
How do I fix it?
You can try this:
<%= f.select :visibility, collection_for_visibility_select, :selected => (f.object.visibility.nil? ? Contact::VISIBILITY_PUBLIC : f.object.visibility), :include_blank => false %>
It will read the value from the model first, and if it is nil, will use the default value.
I'd like to add an "Other" option with the value -1 in this select:
<%= f.select :id, options_from_collection_for_select(#vehicles, :id, :model, "#used_vehicles[i]") %>
Should I replace #vehicles with something else?
If you can, you should use the already existant behaviour in Rails, which is to use the include_blank option:
<%= f.select(:id, #vehicles.collect {|p| [ p.name, p.id ] }, {include_blank: 'Other'}) %>
You can check the documentation also.
I'm trying to create a select box that takes data from my db. I'm having trouble setting this up. I tried this code:
<%= f.fields_for :unit do |u| %>
<%= u.label :name %>
<%= u.select :name, :class => "ingredient_unit", :prompt => "Please Select" %>
<% end %>
but I'm missing the part of the choices, I don't know how to pull them out of the database. I tried using collection_select, which worked, but then the class option wasn't working... collection_select went like this:
<%= u.collection_select :unit, Unit.all, :id, :name, :class => "ingredient_unit", :prompt => "Please Select" %>
I also don't understand what the first symbol means (:unit), it seems to be setting the html id and name, so that can be anything I want it to be?
You should look at the documentation for collection_select and select. But to answer your question, for the select part, you forgot to pass the list of options to choose from. You also need to swap the order for prompt and class since prompt is an option for the helper and class is an html option
<%= u.select :unit_id, Unit.all.map { |u| [u.name, u.id] }, { :prompt => "Please Select" }, { :class => "ingredient_unit" } %>
For the collection select
<%= u.collection_select :unit_id, Unit.all, :id, :name, { :prompt => "Please Select" }, { :class => "ingredient_unit" } %>
The first parameter passed to both helper is the column name where you want the selected answer to be saved. The 2 codes above just shows 2 different ways to generate the same select tag.
The first symbol tells it which field to populate with the id returned from the user selection.
Also, you should wrap your class section in {}
:unit refers to the model attribute that you're using for the select element. Yes, it will setup the name/id of the element (and name is the most important for the params hash).
To set a class in the collection_select, specify it as a hash as that helper takes it as an html_option.
<%= u.collection_select :unit, Unit.all, :id, :name, { :prompt => "Please Select" }, { :class => "ingredient_unit" } %>