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 id0.If you want to give an option that doesn’t correspond to a real choice then you have two options:
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
promptversion 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 nilNote that the
include_blankmethod is very similar to the following code, except lots more readable: