Rails Select helper in form required True not working

本小妞迷上赌 提交于 2021-02-09 11:41:21

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/29677225/rails-select-helper-in-form-required-true-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!