title: Rails: Setting a value to input field if variable exists
body: <p>I'm trying to set a value to two text field inputs when an id is passed to the new action in the controller. The current code works but I'm wondering if the code can be shortened.</p>
  
  <p><strong>Current code</strong></p>
  
  <p>View</p>
  
  <pre><code>&lt;div class="custom-input"&gt;
    &lt;% if @debtor %&gt;
      &lt;input type="text" name="debtor_name" class="form-control" id="debtor-search-form" value='&lt;%= @debtor.name %&gt;'&gt;
    &lt;% else %&gt;
      &lt;input type="text" name="debtor_name" class="form-control" id="debtor-search-form"&gt;
    &lt;% end %&gt;
  &lt;/div&gt;
  &lt;% if @debtor %&gt;
    &lt;%= f.text_field :debtor_id, class: "form-control", value: @debtor.id %&gt;
  &lt;% else %&gt;
    &lt;%= f.text_field :debtor_id, class: "form-control" %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>I tried removing the if-else part to make the code shorter</p>
  
  <p><strong>Shorter code</strong></p>
  
  <p>View</p>
  
  <pre><code>&lt;div class="custom-input"&gt;
      &lt;input type="text" name="debtor_name" class="form-control" id="debtor-search-form" value='&lt;%= @debtor.name if @debtor %&gt;'&gt;
  &lt;/div&gt;
  &lt;%= f.text_field :debtor_id, class: "form-control", value: @debtor.id if @debtor %&gt;
  </code></pre>
  
  <p>When no debtor is passed, the shortened code results to the first input having a "hanging" value tag (i.e. it just shows value. no equal sign after it)</p>
  
  <pre><code>&lt;input type="text" name="debtor_name" class="form-control ui-autocomplete-input" id="debtor-search-form" value autocomplete="off"&gt;
  </code></pre>
  
  <p>while the second input disappears.</p>
  
  <p>Is a "hanging" value tag ok? I inspected the html and the "hanging" value tag resolves to value="" if i click 'edit as html' </p>
  
  <p>Is there a way to shorten the code or should i just stick with it?</p>