Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Normally, you will probably use the <code>select</code> helper.</p> <p>Reference: <a href="http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select" rel="nofollow">api.rubyonrails.org</a></p> <p>In this case, this:</p> <pre class="lang-ruby prettyprint-override"><code>select("post", "category", Post::CATEGORIES, {:disabled =&gt; 'restricted'}) </code></pre> <p>could become:</p> <pre class="lang-html prettyprint-override"><code>&lt;select name="post[category]"&gt; &lt;option&gt;&lt;/option&gt; &lt;option&gt;joke&lt;/option&gt; &lt;option&gt;poem&lt;/option&gt; &lt;option disabled="disabled"&gt;restricted&lt;/option&gt; &lt;/select&gt; </code></pre> <p>Instead of supplying an array of disabled options, using the <code>collection_select</code> method, you can also supply a Proc:</p> <pre class="lang-ruby prettyprint-override"><code>collection_select(:post, :category_id, Category.all, :id, :name, \ {:disabled =&gt; lambda{|category| category.archived? }}) </code></pre> <p>If the categories “2008 stuff” and “Christmas” return true when the method archived? is called, this would return:</p> <pre class="lang-html prettyprint-override"><code>&lt;select name="post[category_id]"&gt; &lt;option value="1" disabled="disabled"&gt;2008 stuff&lt;/option&gt; &lt;option value="2" disabled="disabled"&gt;Christmas&lt;/option&gt; &lt;option value="3"&gt;Jokes&lt;/option&gt; &lt;option value="4"&gt;Poems&lt;/option&gt; &lt;/select&gt; </code></pre> <hr> <p>You can also supply an array of disabled options in the options hash to the <code>options_for_select</code> method. Reference: <a href="http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_for_select" rel="nofollow">apidock.com</a></p> <p><strong>Example with an array of disabled options</strong></p> <pre class="lang-ruby prettyprint-override"><code>options_for_select(["Free", "Basic", "Advanced", "Super Platinum"], \ :disabled =&gt; ["Advanced", "Super Platinum"]) </code></pre> <p>Gives:</p> <pre class="lang-html prettyprint-override"><code>&lt;option value="Free"&gt;Free&lt;/option&gt; &lt;option value="Basic"&gt;Basic&lt;/option&gt; &lt;option value="Advanced" disabled="disabled"&gt;Advanced&lt;/option&gt; &lt;option value="Super Platinum" disabled="disabled"&gt;Super Platinum&lt;/option&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload