1

リストがあり<select>ます。その中で、ユーザーは一部のオプションにアクセスできません (ユーザーはそれらのオプションを見ることはできますが、選択することはできません)。

これを行うために Ruby on Rails で利用できるヘルパーはありますか?

4

3 に答える 3

3

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>
于 2012-09-03T09:03:12.970 に答える
0

これを試して

<%= f.select :action_item_status, action_item_status, {}, {:disabled => true} %>
于 2012-09-03T09:02:14.887 に答える
0

手動でオプションを構築し、無効な属性を設定し <option disabled="disabled"></option>、無効なオプションを試してください

于 2012-09-03T09:02:31.163 に答える