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' %>
Related
The two snippets of code below, the options_from_collection_for_select works for setting the :selected when i use a set value, but when I use #posts.user_id it fails.
Why is this working?
select_tag "user-dropdown", options_from_collection_for_select(#users, 'id', 'fname', **11**), :class =>'form-control'
But this is not?
select_tag "user-dropdown", options_from_collection_for_select(#users, 'id', 'fname', **#posts.user_id**), :class =>'form-control'
select_tag "user-dropdown", options_from_collection_for_select(#users, 'id', 'fname', **#posts.user_id**), :class =>'form-control'
What is this #posts.user_id you are trying to display in your options ?
select_tag with options_for_select example
An example of using options_for_select with select_tag
select_tag 'user_id', options_for_select(#users.collect{ |u| [u.name, u.id] })
This would generate something like:
<select id="user_id" name="user_id">
<option value="1">Brad</option>
<option value="2">Angie</option>
<option value="3">Jenny</option>
</select>
I'm not sure why, but my form is not showing the options selected on submit, even though the params hash shows that the information is being returned to the page.
Collection select code:
<%= f.collection_select :post_topic_ids, PostTopic.all, :id, :name, {}, { multiple: true, class: 'form-control' } %>
Which renders:
<select multiple="multiple" class="form-control" name="post[post_topic_ids][]" id="post_post_topic_ids">
<option value="1">Psychology</option>
<option value="2">Engineering</option>
<option value="3">Nanotechnology</option>
</select>
Params returned after form validation error
params = {"post"=>{"post_topic_ids"=>["", "1"]}}
Update
I have also tried:
<%= select_tag 'post_topic_ids', options_for_select(PostTopic.all.collect{ |p| [p.name, p.id] }), multiple: true %>
and:
<%= select_tag 'post_topic_ids', options_from_collection_for_select(PostTopic.all, "id", "name"), multiple: true %>
Which renders:
<select name="post_topic_ids[]" id="post_topic_ids" multiple="multiple"><option value="1">Psychology</option>
<option value="2">Engineering</option>
<option value="3">Nanotechnology</option></select>
you need to specify which element is selected a third parameter
<%= select_tag 'post_topic_ids', options_for_select(PostTopic.all.collect{ |p| [p.name, p.id] }, --->selected_element<---), multiple: true %>
look at http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_for_select for some examples.
<%= form_for(:offer,:url=>{:controller=>'offers',:action=>'combo'}) do |f|%>
<%= f.select :catId_get, options_from_collection_for_select(#categories, "id", "name"), prompt: "Select Category" %>
I am new in rails.I have a dropdown where all categories are there.When i select a category from this dropdown i want to get its category id in my controller,so that i can use that id for it's child dropdown.
Select
Each select option in HTML has two values -- the value and the label:
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
It's only the value which is passed to your controller. This means if you are able to create the select tag in your Rails app with the correct value / label setup, it will pass the correct data you require.
Rails
Here's how I'd handle it:
<%= form_for :offer, offers_combo_path do |f|%>
<%= f.collection_select :cat_id, #categories, :id, :name, prompt: "Select Category" %>
This will pass the following params to your categories_controller:
#app/controllers/categories_controller.rb
class CategoriesController < ApplicationController
def combo
params[:offer][:cat_id]
end
end
Recommendation
I'd actually recommend you use the form_tag helper for this, rather than form_for. Reason being that form_for is mainly for ActiveRecord objects, and although you can use :symbols in the helper, you will really need to use a much less elaborate system
I'd just replace your form_for with the following:
<%= form_tag offer_combo_path do %>
<%= collection_select :cat_id, #categories, :id, :name, prompt: "Select Category" %>
<% end %>
Your id should be accessible by
params[:offer][:catId_get]
in your controller.
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}]
} %>