【问题标题】:Front end validation for rails select fieldRails 选择字段的前端验证
【发布时间】:2015-12-14 08:36:01
【问题描述】:

选择字段:

<%= f.select :study_material_type, StudyMaterial::TYPES.map{|v| [v,v]}, selected: f.object.try(:study_material_type) , required: true,include_blank: "Please select a Study Material"%>

我想验证 :study_material_type 选择字段的存在。我该怎么做?

【问题讨论】:

  • 你想在这里验证什么?

标签: ruby-on-rails ruby-on-rails-3 select


【解决方案1】:

Select 接受 5+ 个选项,而 required 验证是 html_option

select(object, method, choices = nil, options = {}, html_options = {}, &block)

将 html 参数作为html_options 放入{ }。这应该适合你。

<%= f.select :study_material_type, StudyMaterial::TYPES.map{|v| [v,v]}, {include_blank: "Please select a Study Material"}, {required: true } %>

Here是一个让你更好理解的好资料。

【讨论】:

  • undefined local variable or method required' for #:0x0000000aea60a0>`
  • @sree 已编辑。再试一次
  • 我只是编辑了答案,我已经在我的机器上工作了 &lt;%= f.select :study_material_type, StudyMaterial::TYPES.map{|v| [v,v]}, {include_blank: "Please select a Study Material"}, {required: true } %&gt; 这现在应该可以工作了。
猜你喜欢
  • 2015-06-27
  • 1970-01-01
  • 2021-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多