所需的Rails表单字段不会提醒“请填写此字段”

时间:2017-08-21 20:03:51

标签: ruby-on-rails forms drop-down-menu

我是Rails表单的新手并拥有以下表单字段:

    <%= f.label :name, class: "black-text" %>:
    <%= f.text_field :name, class: "white task-card", :required => true %>
    <%= f.label :foo, class: "black-text" %>:
    <% options = [['Option A', 1], ['Option B', 2], ['Option C', 3], ['Option D', 4]] %>
    <%= f.select :foo, options_for_select(options), {}, {:include_blank => true, :required => true, class: "white task-card"} %>

如果留空,则文本输入字段不会前进并正确警告用户“请填写此字段”,但选择选项不会。如果将选择字段保留为其默认空白值,则页面将不会前进,因此它以这种方式工作,但是没有警告用户为什么没有发生任何事情。如何为选择字段制作浏览器的标准HTML5“请不要填写此字段”警报工作?

谢谢!

2 个答案:

答案 0 :(得分:0)

您需要另一个哈希来提供html选项,因为第一个哈希(例如,包含include_blank选项)用于标记选项:

<% options = [['Option A', 1], ['Option B', 2], ['Option C', 3], ['Option D', 4]] %>
<%= f.select :foo, options_for_select(options), 
    {:include_blank => true, class: "white task-card"}, { :required => true } %>

顺便说一句,我不认为include_blank选项,因为该字段是必需的

答案 1 :(得分:0)

我认为存在语法问题:

<%= f.select :foo, options_for_select(options), {:include_blank => 
        true, class: "white task-card"}, { :required => true } %>

希望这有帮助