include blank : true not working in select rails - ruby-on-rails

<%= select :used_car, :car_make_id, #all_makes.map { |m| [m.title, m.name] }, {:include_blank => true,:prompt=>"Select Make"}%>
<%= select :used_car, :car_model_id, [], {:include_blank => true,:prompt=>"Select Model")}, size: 10, :class=>"form-control select"%>
Car models list updating on basis on car make selection
Issue is that when I select car make, prompt of car model remove and first option come
how i resolve it?

Here is the documentation.
http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-select
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select
Try f.select :car_model_id, ... instead. Also try using nil for choices, or [[]] for choices.
But first, you have an extra stray parenthesis ')' in this line:
<%= select :used_car, :car_model_id, [], {:include_blank => true,:prompt=>"Select Model" ) }
You have to remove that parenthesis. It doesn't go with anything.

Related

rails 4 select with selected option not working

In my rails 4 form, I have one field to select values.
<%= f.select(:owner_user_id, Item.find(session[:login_users_item_id]).try(:users).order_by_fullname.collect {|u| [ u.full_name, u.id ] } , {:selected => current_user.full_name} )%>
But, the above selected option not showing the above value. In console that value is getting properly.Now,The first item is showing in the select box.
How can I get the value in the selected option as the default value?
<%= f.select(:owner_user_id, Item.find(session[:login_users_item_id]).try(:users).order_by_fullname.collect {|u| [ u.full_name, u.id ] }, selected: f.object.owner_user_id)%>
Try this
The third argument to f.select method is the selected value, and there you can pass the value that you would like to see selected. In your case: current_user.full_name
<%= f.select(:owner_user_id, Item.find(session[:login_users_item_id]).try(:users).order_by_fullname.collect {|u| [ u.full_name, u.id ] }, current_user.full_name)%>
You can also use options_for_select helper.
Example :
<%= f.select :owner_user_id, options_for_select(Item.find(session[:login_users_item_id]).try(:users).order_by_fullname.pluck([ :full_name, :id ]), f.object.owner_user_id) %>

Validates the presence of an option

I have a form that I want to validate if an option has been selected.
I do not want to accept the default starting option, "Please Select a Product"
I using this now and get a uninitialized contant error. I believe I am writing the syntax wrong.
Controller:
validates :product_name, :presence => { :unless => (product_name = "Please Select a Product")}
View:
<span class="span5 pagination-right">
<%= f.label "Product" %>
<%= f.select :product_name, options_for_select([ ["Please Select a Product"] ]) %>
</span>
How am I supposed to have the option written?
Thank you
The product_name is changed like this:
<script>
$(document).ready(function() {
$('#ticket_product_name').html("<option>Please Select a Product</option>");
$('#ticket_firmware_version_string').html("<option>Please Select a Firmware</option>");
$('#category').change(function(){
$('#ticket_product_name').html("<option>Please Select a Product</option>");
$('#ticket_firmware_version_string').html("<option>Please Select a Firmware</option>");
if ($('#category').val() == "blah")
{
$('#ticket_product_name').append("<option>blah</option>");
$('#ticket_product_name').append("<option>blah</option>");
}
else if ($('#category').val() == "another category")
{
$('#ticket_product_name').append("<option>blah product</option>");
}
until end of options, end script.
I think what you are looking for is a placeholder for this select. In this case, I used a disabled option:
<% options = options_for_select(["Select an option", ["Product #1",1], ["Product #2",2]], disabled: "Select an option") %>
<%= f.select :product_name, options %>
You should use include_blank from the select helper, like this:
<%= f.select :product_name, options_for_select([["opt1",1],["opt2",2]]), include_blank: true %>
and then, on model
validates :product_name, :presence => true
#MrYoshiji commented something truthful in my answer - if you are not allowing it to be blank, the easy way is simply not add a blank option in the select
I would recommend to use the :prompt option for your select helper (the documentation on this isn't quite straight forwward, but it's ok in this version of the same thing: http://apidock.com/rails/ActionView/Helpers/FormTagHelper/select_tag). This allows you to define a helper string of text to encourage the user to select an option, but will submit a nil value back when the form is submitted.
<%= f.select :product_name, PRODUCT_NAME_OPTIONS, prompt: 'Select an option' %>
The next thing I'd recommend is... instead of validating presence of on the model, do a validates_inclusion_of. This way you can define the set of options in the model and then use that same set of options in the select helper, above, as well is in your validation. This way you know the form wasn't manipulated to include a different option and it actually keeps things a bit DRYer.
Product
PRODUCT_NAME_OPTIONS = {'opt 1' => 'opt1', 'opt 2' => 'opt2'}
validates_inclusion_of :product_name, :in => PRODUCT_NAME_OPTIONS.values, :message => 'choose from the available options'
Instead of making this too complicated, I just added a length validation since the default is much longer then the product names. Simple is best, however there should be a string validator option.
validates :product_name, presence: true, length: {
maximum: 21,
too_long: ": Must select a product"
}

Choose order Rails records, or show " ALL " in first

I'm searching for a method to choose the first record of a grouped_collection_select, or to show " ALL " in the first choice.
<%= f.collection_select :marque_id, Marque.all, :id, :name_upper, :include_blank => 'ALL', :prompt => 'Mark' %>
<%= f.grouped_collection_select :modele_id, Marque.all, :modeles, :name_upper, :id, :name_upper, :prompt => 'Model', :include_blank => 'ALL' %>
The " :include_blank => 'ALL' " work fine for the collection_select, but not for the grouped_collection_select.
For the grouped_collection_select, it only show a blank field at the first place.
I tried to put a blank record in the database, or to put " ALL " in the database with a lot of symbol like : " # ALL # " ... Didn't work with number ( 147, 156, 159 [...], # ALL #,...)
EDIT:
Or maybe I can add a field in each subcategories, and define that "First" ? But I want to keep everything ordering by "name ASC".
I already put
default_scope order('name ASC')
in my Modele.rb
I found how to do what I wanted.
I just had to add
$('#search_modele_id').preprend('<option value="" selected="selected">ALL</option>');
to my jQuery code, and put the value as " BLANK " (really important in my case).
And put
:include_blank => true
Instead of
:include_blank => 'ALL'
In the search form
Problem solved.
Thanks anyway!

Rails - how to validate f.select field?

I have simple select field
f.select(:name, [["- Choose Name -", 0]] + People.all.map{ |c| [c.name, c.id] })
How can I validate, if some name was selected?
I tried this rule:
validates :name, :presence => {:message => 'Name cannot be blank.'}
But if I send form without choosing the name, the form is sent...
So I would like to ask you, how can I validate the select field?
In your example, people are selecting a name, they're selecting the name - Choose Name - with id 0.
If you want to give an option that doesn't correspond to a real choice then you have two options:
f.select(:name, People.all.map{ |c| [c.name, c.id] }, :include_blank => "- Choose Name -")
f.select(:name, People.all.map{ |c| [c.name, c.id] }, :prompt => "- Choose Name -")
These are basically identical. Both will display an option at the top of the select which won't result in a value being set. However, the prompt version will only display this extra option if there isn't a current value selected, i.e. if used on an edit form it won't let the user clear their selection by setting it back to nil
Note that the include_blank method is very similar to the following code, except lots more readable:
f.select(:name, [["- Choose Name -", nil]] + People.all.map{ |c| [c.name, c.id] })

Ruby on Rails: how do I use a default placeholder thing in a select_tag?

<%= select_tag(:services,
options_from_collection_for_select(Service.all, :id, :name))%>
And it displays all the services...
But I want it to be something like:
Select a service
Service1
Service2
Service3
Service4
Most of the time, you don't want to append anything to the array directly; either of these is a cleaner solution:
Use :prompt => "Placeholder" if you want the placeholder to show up only when the attribute is nil at the time the form is rendered. It will be selected by default, but nothing will be saved if user submits. If the attribute is already populated [possibly because a) there's a default value or b) it's an edit form], the placeholder item will be omitted from the list entirely.
Use :include_blank => "Placeholder" if you want to include the placeholder in the rendered list at all times.
<%= select_tag(:services,
Service.all.collect { |c| [c.id, c.name] }.
insert(0, "Select a Service"))%>
As answered for the question, this is for Rails 2.3. For Rails 3, see Prathan Thananart's answer.
The better way to do this would be to use the :prompt parameter. Something like:
select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, {:prompt => 'Select Person'})
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html

Resources