I am using the Rails 4.1 enum field
class User
enum category: [ :client, :seller, :provider ]
end
When the user signs up, he chooses from a select box his category. The default is empty, because I want to force the user to choose one option.
If the user does not select any option, I would like to return to the form with a validation message. Here is the select box code in sign up form
<%= f.select :category, [], {}, class: "form-control" do %>
<option value="99">Choose an option</option>
<% User.categories.each do |cat,code| %>
<option value="<%= code %>" <% if params["user"] && code.to_s == params["user"]["category"] %>selected='selected'<%end%> ><%= t(cat) %></option>
<% end %>
<% end %>
When the controller creates the user, instead of adding a validation error to the record, it raises an exception. How to avoid this?
ArgumentError - '99' is not a valid category:
(gem) activerecord-4.1.1/lib/active_record/enum.rb:103:in `block (3 levels) in enum'
(gem) activerecord-4.1.1/lib/active_record/attribute_assignment.rb:45:in `_assign_attribute'
(gem) activerecord-4.1.1/lib/active_record/attribute_assignment.rb:32:in `block in assign_attributes