我在 Rails 应用程序中使用 simple_form。我想在下拉列表中禁用特定值。
这是代码的一部分
= simple_form_for(@organization,url: admin_organization_path) do |f|
= f.input :hospital_name, input_html: { class: "form-control"}
= f.input :parent, collection: @organizations, input_html: { class: "form-control", id: "chosen-select-speciality"}
我尝试使用
:disabled => @organizations.first
但我失败了。有没有其他方法可以使用。请帮助我。谢谢。
最佳答案
选择框的 Simpleform 构建器使用每个选项的值与 disabled 属性的值进行比较,因此您只需使用组织的 id 即可禁用所需的选项:
= simple_form_for(@organization,url: admin_organization_path) do |f|
= f.input :hospital_name, input_html: { class: "form-control"}
= f.input :parent, collection: @organizations, input_html: { class: "form-control", id: "chosen-select-speciality"}, disabled: @organizations.first.id
如果要禁用选择框的多个选项,可以手动构建选项列表内联或使用帮助程序并将其用作输入的属性:
= f.input :parent, collection: @organizations.map{|o| [o.id, o.name, {disabled: o.id.in?([1,21,10])}]}, input_html: { class: "form-control", id: "chosen-select-speciality"}
关于ruby-on-rails - 如何使用simple_form禁用下拉选择中的特定值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35177071/