nulltek
nulltek

Reputation: 3337

Rails Select helper in form required True not working

I have a Rails 3.2.21 app in which I'm using a select helper in a form like so:

<%= f.select :phys_option, options_for_select([["N/A", "n/a"], ["No", "no"], ["Yes", "yes"]], :selected => @call.phys_option), :include_blank => true, :required => true, :class => 'select' %>

This works with basic functionality for selecting an option, including a blank option, etc. But what doesn't work is the :required => true or the :class => 'select'. I can submit the form even when the selection is blank and my class for the select2 gem select doesn't work on this helper method.

Is my syntax wrong or am I missing something? I can call a model validation to ensure the fields are filled out, but I'd much rather avoid more model validations and try to use the :required => true to force a selection.

Any thoughts on why this isn't working?

If you need further detail and/or code, please let me know.

Upvotes: 3

Views: 3771

Answers (2)

manishnitte
manishnitte

Reputation: 211

Problem : Selected item is not displayed in edit

Solution

<div class="field form-group">
      <%= form.label :grade %>
      <%= form.select(:grade, options_for_select(['A', 'B', 'C', 'D', 'E', 'F'], :selected => form.object.grade), {:include_blank => 'Select Grade'}, class:"form-control", placeholder:"Grade", onchange: "loadRemarksBasedOnGrade()") %>
    </div>

Note: Use :selected => form.object.grade inside options_for_select

Upvotes: 0

RAJ
RAJ

Reputation: 9747

Try this:

<%= f.select :phys_option, options_for_select(
                            [["N/A", "n/a"], ["No", "no"], ["Yes", "yes"]],
                            :selected => @call.phys_option), 
                           {:include_blank => true},
                           {:required => true, :class => 'select'} %>

Upvotes: 6

Related Questions