title: Rails: Best way to conditionally show/hide a form field?
body: <p>I have a model <strong>Document</strong> which has attributes <strong>kind</strong> and <strong>context</strong>. Kind is an integer that's used as as enum (using the excellent <code>active_enum</code> gem). Context only applies to documents of kind '2', i.e. if a document is any kind other than 2, <em>context</em> will be blank.</p>
  
  <p>So in the form page to create a new Document, I have a <code>&lt;select&gt;</code> to choose the kind, and a textarea for the context, which is initially hidden:</p>
  
  <pre><code>&lt;%= form_for @document do |f| %&gt;
  
    ...  
  
    &lt;%= f.text_area :context, placeholder: 'Context', style: 'display:none' %&gt;
  
    &lt;%= f.select :kind, Document.active_enum_for(:kind).to_select %&gt;
  
    ...
  
  &lt;% end %&gt;
  </code></pre>
  
  <p>And the textarea is shown and hidden using jQuery's show() and hide() methods in a function that's bound to the change() event on the dropdown.</p>
  
  <p>So far, so good. But on the <strong>edit</strong> page for documents, I don't want the context textarea to <em>always</em> be hidden on the initial page load, because we may be editing a document of kind <strong>2</strong>. So I want the textarea to initially be shown if we're editing a document of kind 2, but hidden in other cases.</p>
  
  <p>Here's what I have now:</p>
  
  <pre><code>&lt;% if @document.kind == 2 %&gt;
    &lt;%= f.text_area :context, placeholder: 'Context' %&gt;
  &lt;% else %&gt;
    &lt;%= f.text_area :context, placeholder: 'Context', style: 'display:none' %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>Is there not a better way of doing this? This feels a bit wordy and redundant to me; is there no way I can just have one call to <code>f.text_area</code> and include the <code>style:</code> option conditionally?</p>
  
  <p>Or am I overthinking this?</p>