I have this select with simple_form:
<%= f.input :theme, :collection => ["#{t('.text_1')}", "#{t('.text_2')}", "#{t('.text_3')}", "#{t('.text_4')}", :value_method => lambda { |n| n } %>
The html is:
<select class="select required" id="inquiry_theme" name="inquiry[theme]"><option value="">Choose a topic related to your query</option>
<option value="Text1">Text1</option>
<option value="Text2">Text2</option>
<option value="Text3">Text3</option>
<option value="Text4">Text4</option>
I want set the value with a range numbers instead text e.g.:
<select class="select required" id="inquiry_theme" name="inquiry[theme]"><option value="">Choose a topic related to your query</option>
<option value="1">Text1</option>
<option value="2">Text2</option>
<option value="3">Text3</option>
<option value="4">Text4</option>
Is possible with a lambda in :value_method?
Thank you!
Given your example, I think using value_method is a little overkill.
<%= f.input :theme, :collection => [[1, "#{t('.text_1')}"], [2, "#{t('.text_2')}"], [3, "#{t('.text_3')}"], [4, "#{t('.text_4')}"]] %>
Though I'd write this as
<%= f.input :theme, :collection: (1..4).map { |i| [i, t(".text_#{i}")] }.unshift([nil, 'Choose a topic related to your query'] %>
Based on your example output , I write like this it may be useful for you.
<%= f.select :theme,options_for_select((1..4).to_a.map{|x| [x,"Text#{x}"]}.insert(0, "Choose a topic related to your query")) ,:id=>"inquiry_theme" %>
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 have a select that take values from database.
<%= select_tag :location, options_from_collection_for_select(Country.all, :id, :country_name), { :class => 'selectpicker' } %>
So i get all countries from database.
How i can do for add a custom value(for example Any, with value 0), to this select list taken from database ?
For example now i have:
<select>
<option value="UK">UK</option>
</select>
and i want get this:
<select>
<option value="0">Any</option>
<option value="UK">UK</option>
</select>
Thanks.
You can use the either prompt or include_blank options (FormOptionsHelper) as follows:
<%= select_tag :location, options_from_collection_for_select(Country.all, :id, :country_name), :prompt => 'Any', :class => 'selectpicker' %>
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}]
} %>