本文介绍了删除由 SimpleForm 生成的选择字段的空白选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码:

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

Choices[Categories"] 只是键=> 值对的散列.

Choices["Categories"] is just a hash of key=>value pairs.

SimpleForm 生成一个包含所有需要选项的选择字段,但它也会将第一个选项设为空白.
该空白选项存在于 SimpleForm 生成的所有选择字段中.

SimpleForm generates a select field with all needed options, but it also makes the first option blank.
This blank option is present in all select fields that were generated by SimpleForm.

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

But I don't want to have a blank option. Is there a way to get rid of it?

类似于 :allow_blank_option =>错误?

我试图对该属性进行存在验证,希望 SimpleForm 能够检测到它,但没有帮助.

I tried to make a presence validation of this attribute hoping that SimpleForm will detect it, but it didn't help.

推荐答案

你可以传递一个 include_blank: false, include_hidden: false 选项:

You can pass a include_blank: false, include_hidden: false option:

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

这篇关于删除由 SimpleForm 生成的选择字段的空白选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 06:13