Ask A Question

Notifications

You’re not receiving notifications from this thread.

Select Form Helper Required True Not Working

shakycode asked in Rails

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!

Reply

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'} %>

Reply

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.

Reply

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. :)

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,563+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.