rails选择字段的前端验证

时间:2015-12-14 08:36:02

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

选择字段:

<%= 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选择字段的存在。我怎么能这样做?

1 个答案:

答案 0 :(得分:-1)

选择包含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是一个很好的材料,让您更好地理解。