hellion
hellion

Reputation: 4850

Adding disable to certain select options in simple_form

A user can chose 5 options. Once they choose one, they can not choose it again. Currently, I simply remove previously used options from the list...so, they can only choose from previously unused ones. The only thing I don't like about this is that as the user is adding a new record to the database, they might wonder why options seem to be missing from the list.

One idea I had was to leave previously used options in the list but cross them out and make them disabled.

Is it possible to disable (and/or add a class) to only certain options in a select? Simple_form seems to have an option_html helper but, didn't see it documented.

Upvotes: 10

Views: 5884

Answers (2)

hellion
hellion

Reputation: 4850

Turns out this is quite easy to do. Rails supports it...

 <%=  f.association :ying, collection: @yangs, :disabled => @used_yangs %>

Upvotes: 15

everm1nd
everm1nd

Reputation: 731

It works for me only when I pass ids into disabled option. Like this:

= f.association :stuff, disabled: Stuff.where(enabled: false).map(&:id)

Using Rails 4.2.3

It's correct regarding Rails docs. Check :disabled description here: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html

Upvotes: 5

Related Questions