删除SimpleForm生成的选择字段的空白选项

时间:2013-02-28 18:48:28

标签: ruby-on-rails

我有这段代码:

= f.input :category, :as => :select, :label => false, :collection => Choices["Categories"]

选择[“类别”]只是key =>值对的散列。

SimpleForm生成一个包含所有必需选项的选择字段,但它也使第一个选项为空。
这个空白选项出现在SimpleForm生成的所有选择字段中。

但我不想有空白选项。有没有办法摆脱它?

:allow_blank_option => false

之类的东西

我尝试对此属性进行状态验证,希望SimpleForm能够检测到它,但它没有帮助。

3 个答案:

答案 0 :(得分:87)

您可以传递include_blank: false, include_hidden: false选项:

= f.input :category, :as => :select, :label => false, :collection => Choices["Categories"], include_blank: false, include_hidden: false

答案 1 :(得分:1)

或者您可以在模型中自定义回调操作以删除数组参数中的任何空字符串,假设参数名称为" types":

before_validation :remove_empty_string

def remove_empty_string
  types.reject! { |l| l.empty? }
end

答案 2 :(得分:0)

要从选择中删除空白字段,必须显示选择的内容,因此添加import java.util.regex.Matcher; import java.util.regex.Pattern; public class CountI { public static void main(String[] args) { String input = "IIiaIii"; String regex = "([A-Za-z])\\1+"; Pattern pattern = Pattern.compile(regex , Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(input); String output = matcher.replaceAll("$1"); int result = 0; for(int i = 0; i < output.length(); i++){ if(output.charAt(i) == 73 || output.charAt(i) == 105){ result++; } } System.out.println(result); } } 然后将提示设置为selected: 1之类的内容 最终输出将是 prompt: "Please Select"