When I use:
<%= f.collection_select :parent_id, Parent.all, :id, :title, prompt: true %>
Then Rails generates:
<select name="child[parent_id]" id="child_parent_id">
<option value="">Please select</option>
<option value="1">Parent#1</option>
...
</select>
But I want to disable prompt:
<option value="" disable>Please select</option>
Is there an attribute which disables it?
I don't want to remove it. I want to disable it
prompt will provide an input value, include_blank will not.
ìnclude_blank: 'fill this field out or you shall not pass...'
Remove prompt option and add include_blank: false instead of it. It should remove 'Please select' option.
Related
I want a result like this :
<select dir="rtl">
<option selected disabled>Choose a car</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
With the following code I can only end up with :
<%= f.select(:car, xxxxxx, {:include_blank => 'Choose a car', :disabled => 'Choose a car'}) %>
=>
<select id="xxx" name="xxx">
<option value="">Choose a car</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
The first option is not disabled...
Ito A's answer will not work in Rails 4.2 (Unsure about earlier versions).
From the documentation...
:disabled - can be a single value or an array of values that will be disabled options in the final output.
Therefore, the :disabled option should be given a value that matches the value of one of the options in the collection. So, :disabled => 'volvo' would disable the option with value='volvo'. However, it will not match the include_blank option, because that option is not part of the collection passed into the select method.
Rails select helper does not directly support the desired behavior. However, you can work around it by adding a blank option to the collection as follows.
Create the collection and then add a blank option to it.
car_names = %w(volvo saab mercedes audi)
car_names_with_blank = car_names.map{|c| [c, c]}.prepend(['Choose a car', nil])
In the view:
<%= f.select(:name, car_names_with_blank, {disabled: '', selected: ''}) %>
Here's a link to a Github repository with a working example. The example also shows that Ito A's answer and other answers to similar SO questions will not work.
UPDATED: I Have updated my answer with a solution and additional info.
I believe they add the functionality in Rails 6.
From the pull request:
Enable select tag helper to mark prompt option as selected and/or disabled for required field. Example:
select :post,
:category,
["lifestyle", "programming", "spiritual"],
{ selected: "", disabled: "", prompt: "Choose one" },
{ required: true }
Placeholder option would be selected and disabled. The HTML produced:
<select required="required" name="post[category]" id="post_category">
<option disabled="disabled" selected="selected" value="">Choose one</option>
<option value="lifestyle">lifestyle</option>
<option value="programming">programming</option>
<option value="spiritual">spiritual</option>
</select>
I believe what you are looking for is as below:
<%= f.select(:car, xxxxxx, {:include_blank => 'Choose a car', :disabled => 1}) %>
An application of the above noted solution follows.
create the application helper function
def build_selections(prompt: "Select One", selections: {})
selections.reverse!.push([prompt, nil]).reverse!
end
then use it in your view:
<%= f.select :category_id, build_selections(prompt: 'Select Category',
selections: #category.collect{|x| [x.name, x.id]}),
disabled: '' %>
Help me please to solve problem with collection_select.
When I use:
collection_select(:service, :carmake_id, Carmake.all, :id, :name, include_blank: 'Any')
HTML is:
<select id="service_carmake_id" name="service[carmake_id]">
<option value="">Any</option>
<option value="12">Audi</option>
<option value="16">Porsche</option>
<option value="17">VW</option>
</select>
But I need value="0" for "Any" option.
Is it possible?
Update:
select(:service, :carmake_id, [['Any', 0]] + Carmake.all.collect { |p| [p.name, p.id]})
helped me, but there is railsway? Or I misunderstand something?
This might work:
options = Carmake.all.unshift Carmake.new(id: 0, name: 'Any')
collection_select(:service, :carmake_id, options, :id, :name, include_blank: 'Any')
Although I did not test saving/updating in action.
I am trying to have a <select> be required in my Rails form.
This is my code (elipsis is to make line shorter):
<div class="field">
<p><%= f.label :category, "Category:" %></p>
<%= f.select :category, ['Analytics','Commerce',..., 'Web'], :prompt => '-- Select One --', :required => true %>
</div>
Which outputs
<div class="field">
<p><label for="startup_category">Category:</label></p>
<select id="startup_category" name="startup[category]">
<option value="">-- Select One --</option>
<option value="Analytics">Analytics</option>
<option value="Commerce">Commerce</option>
<option value="Content Management">Content Management</option>
<option value="Gaming">Gaming</option>
<option value="Green">Green</option>
<option value="Media">Media</option>
<option value="Social Media">Social Media</option>
<option value="Technology - Software">Technology - Software</option>
<option value="Technology - Hardware">Technology - Hardware</option>
<option value="Web">Web</option></select>
</div>
Putting {:required => true} instead of :required => true gives a syntax error and {:prompt => '-- Select One --', :required => true} renders the page, but without the required="true" in my select tag.
How can I get required="true" in my tag?
try this one....
f.select :category, ['Analytics','Commerce',..., 'Web'], { :include_blank => '-- Select One --' }, :required => true
For Rails 4
f.select :category,
['Analytics','Commerce',..., 'Web'],
{
include_blank: '-- Select One --' ,
required: true
}
I am not sure if it accomplish what you want, but I have two solutions for you. You can use simpleform gem.
OR
Style it:
<label class="required">Category</label>
The in css:
label.required:after{content:"*"}
I'm using
f.collection_select :country_id, Country.all, :id, :name)
which generates
<select name="user[country_id]" id="user_country_id">
<option value="1">Canada</option>
<option value="2">United Kingdom</option>
<option value="3" >United States</option>
</select>
I would like to include a prov-val and code-val attribute to the select so I can dynamically update the province labels:
<select name="user[country_id]" id="user_country_id">
<option prov-val="Province / Territory" code-val="Postal Code" value="1">Canada</option>
<option prov-val="County" code-val="Postcode" value="158">United Kingdom</option>
<option prov-val="State" code-val="ZIP Code" value="2" >United States</option>
Is this possible using a collection_select ?
Not sure if it's possible using collection_select, but I think using select does what you want:
<%= f.select :country_id, Country.all.map {|c| [c.name, c.id, {:'prov-val' => c.prov_val, :'code-val' => c.code_val}]} %>
This assumes that your country object has the prov_val and code_val fields.
You shouldn't be calling the model right from the view.
It is better to use an instance variable instead:
<%= f.select :country_id, #countries.map {|c|
[c.name, c.id, {:'prov-val' => c.prov_val, :'code-val' => c.code_val}]
} %>
Rails 2.3.5, Ruby 1.86
I haven't been able to figure this out. The 'onchange' in the select below is not being written (no onchange written in the HTML). I haven't seen a reference to the syntax being different except in some older examples the onchange is surrounded in brackets:
<%= f.select :directory_id, options_for_select(#directories, #directory_to_select), :onchange => 'folder_lookup()' %>
results in:
<select id="contact_directory_id" name="contact[directory_id]">
<option value="2">test_1</option>
<option value="4">test_2</option>
<option value="33" selected="selected">test_3</option>
</select>
If I simply change "f.select" to "select_tag" the onchange is written correctly (not that I want to do that though):
<%= select_tag :directory_id, options_for_select(#directories, #directory_to_select), :onchange => 'folder_lookup()' %>
results in:
<select id="contact_directory_id" name="directory_id" onchange="folder_lookup()">
<option value="2">test_1</option>
<option value="4">test_2</option>
<option value="33" selected="selected">test_2</option>
</select>
Am I missing a syntax difference for onchange between a select and select_tag helper?
Thanks!
This is what you want:
<%= f.select :directory_id, options_for_select(#directories, #directory_to_select), {}, :onchange => 'folder_lookup()' %>
With select the method signature looks like this select(object, method, choices, options = {}, html_options = {}). onchange is an html_option, since you don't have any options, you need an empty hash so that your last onchange is taken as an html_option.