ruby-on-railssimple-formmulti-selectjquery-chosenslim-lang

How to disable specific value in dropdown select with simple_form?


I am using simple_form in my rails application. I want to disable a particular value in drop-down.

Here is part of the code

= 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"}  

I tried using :disabled => @organizations.first but i was failed. Is there any other method to use.Kindly help me. thanks.


Solution

  • Simpleform builder for select box uses value of the each option to compare with value of disabled attribute, so you just simple have to use id of the organization to disable desired option:

    = 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
    

    If you want to disable several options for selectbox, you can manually build options list inline or using helper and use it as attribute for input:

    = 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"}