Skip to main content
Ask A Question
Notifications
You’re not receiving notifications from this thread.
Subscribe

Select Form Helper Required True Not Working

Rails • Asked by shakycode

I'm doing a very simple select field in a form which looks like this:

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

This works with basic functionality, includes a blank option, and allows me to select the proper item from the array. But what doesn't work is the :required => true and the :class => 'select' arguments. I can submit the form without selecting anything and my select class (using select2 for dropdowns) doesn't work. Should these arguments be in a hash form instead? Or am I missing something simple here? I need the field to be required but don't want to do a model validation on it if I don't have to.

Thanks in advance, guys!


I figured this out. I had to include the arguments in a hash like so:

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


Ah yes, that's one of those odd gotchas because it accepts two hashes where as most of these methods accept only one. It's a tag with a whole lot of options, so it kind of makes sense, but seems a bit overly complicated.


I agree that it's a bit overly complicated. Something I'm trying to get away from. Had I been thinking and not in a hurry to add a field to a bloated model I would have used a boolean instead. Would have made life much easier. :)


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 30,005+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.