javascript - Disable the dropdown value which already exist in database -


can guide me solution. example , there dropdown down in form contains animals name. if user select 1 animal that, saved in dropdown. again in selection process, saved data want become disabled. other options able select. solution want.

you can achieve helper method view, or set instance variables in controller here pseudo code going.

def disabled_animals    animal.where(already_selected: true).pluck(:name) end  def selectable_animals   animal.all.pluck(:name) - disabled_animals end 

the idea 2 arrays, 1 without disabled animals , 1 disabled animals , pass select helper method, in view.:

select("post", "animal", selectable_animals, {disabled: disabled_animals})

which produce this:

<select name="post[animal]" id="post_animal">      <option value=""></option>      <option value="joke">joke</option>      <option value="poem">poem</option>      <option disabled="disabled" value="zebra">zebra</option>  </select> 

reference: http://api.rubyonrails.org/classes/actionview/helpers/formoptionshelper.html


Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -