How to disable some of the options in a select?

I have a <select> list. In it, the user does not have access to some of the options (user can see those options but can not select them).

Is there any helper available in Ruby on Rails to do this?

Upvotes: 1

Views: 3083

Answers (3)

ronalchn
ronalchn

Reputation: 12335

Normally, you will probably use the select helper.

Reference: api.rubyonrails.org

In this case, this:

select("post", "category", Post::CATEGORIES, {:disabled => 'restricted'})

could become:

<select name="post[category]">
  <option></option>
  <option>joke</option>
  <option>poem</option>
  <option disabled="disabled">restricted</option>
</select>

Instead of supplying an array of disabled options, using the collection_select method, you can also supply a Proc:

collection_select(:post, :category_id, Category.all, :id, :name, \
                  {:disabled => lambda{|category| category.archived? }})

If the categories “2008 stuff” and “Christmas” return true when the method archived? is called, this would return:

<select name="post[category_id]">
  <option value="1" disabled="disabled">2008 stuff</option>
  <option value="2" disabled="disabled">Christmas</option>
  <option value="3">Jokes</option>
  <option value="4">Poems</option>
</select>

You can also supply an array of disabled options in the options hash to the options_for_select method. Reference: apidock.com

Example with an array of disabled options

options_for_select(["Free", "Basic", "Advanced", "Super Platinum"], \
                   :disabled => ["Advanced", "Super Platinum"])

Gives:

<option value="Free">Free</option>
<option value="Basic">Basic</option>
<option value="Advanced" disabled="disabled">Advanced</option>
<option value="Super Platinum" disabled="disabled">Super Platinum</option>

Upvotes: 3

Amar
Amar

Reputation: 6942

manually construct options and set disabled attributes <option disabled="disabled"></option> and try disabled option

Upvotes: 0

GBD
GBD

Reputation: 15981

Try this

<%= f.select :action_item_status, action_item_status, {}, {:disabled => true} %>

Upvotes: 0

Related Questions