【问题标题】:Rails Select helper in form required True not workingRails Select helper in form required True 不工作
【发布时间】:2015-04-16 14:07:16
【问题描述】:

我有一个 Rails 3.2.21 应用程序,我在其中使用 select 帮助程序,格式如下:

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

这适用于选择选项的基本功能,包括空白选项等。但不起作用的是:required =&gt; true:class =&gt; 'select'。即使选择为空白并且我的 select2 gem select 类在此帮助方法上不起作用,我也可以提交表单。

是我的语法错误还是我遗漏了什么?我可以调用模型验证来确保填写字段,但我宁愿避免更多的模型验证并尝试使用:required =&gt; true 来强制选择。

关于为什么这不起作用的任何想法?

如果您需要更多详细信息和/或代码,请告诉我。

【问题讨论】:

    标签: forms ruby-on-rails-3


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 成功了!我想我需要将它作为散列传递,但是在一个散列中传递所有参数并且它不能正常工作。感谢您的帮助!
    • 遇到select_tag怎么办?它显示参数计数错误。
    猜你喜欢
    • 2015-12-07
    • 2016-02-16
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多