title: Ruby on Rails 3 - How Can I Check a radio_button_tag In My Controller?
body: <p>I have a radio button on a contact form where someone can select 1 of 5 values.  I do not have a default value for the radio button.  I am using form_tag since this data will not be stored on a database.</p>
  
  <p>Here is my code for the radio button:</p>
  
  <pre><code>    &lt;%= label_tag "Purpose:" %&gt;&amp;nbsp;
      &lt;%= radio_button_tag :purpose, '1' %&gt;&lt;%= label_tag :purpose_feedback, 'Suggestions' %&gt; 
      &lt;%= radio_button_tag :purpose, '2' %&gt;&lt;%= label_tag :purpose_prayer, 'Prayer Request' %&gt;
      &lt;%= radio_button_tag :purpose, '3' %&gt;&lt;%= label_tag :purpose_praise, 'Testimony' %&gt;
      &lt;%= radio_button_tag :purpose, '4' %&gt;&lt;%= label_tag :purpose_bug, 'Defects/Bugs' %&gt;
      &lt;%= radio_button_tag :purpose, '5' %&gt;&lt;%= label_tag :purpose_other, 'Other' %&gt;
  </code></pre>
  
  <p>I have the radio button with six text fields on my form.  I do error checking field by field in my controller, starting with the radio button which is at the top of the form.  The error checking is checking all my fields correctly.  However if I select one of the radio button values for params[:purpose] the value is populated properly but the radio button is not checked when the view is displayed.  If I select purpose and enter values for three text fields for example, the values of the text fields are still on the form but the radio button is unchecked even though purpose has a value.</p>
  
  <p>I found this link <a href="https://stackoverflow.com/questions/12161135/how-to-set-the-value-of-radio-button-after-submitting-form">How to set the value of radio button after submitting form?</a> and developed the following code in my controller:</p>
  
  <pre><code>  def check_radio_button
  
      case params[:purpose]
      when '1'
        radio_button_tag(:purpose, '1', :checked =&gt; true)
      when '2'
        radio_button_tag(:purpose, '2', :checked =&gt; true)
      when '3'
        radio_button_tag(:purpose, '3', :checked =&gt; true)
      when '4'
        radio_button_tag(:purpose, '4', :checked =&gt; true)
      when '5'
        radio_button_tag(:purpose, '5', :checked =&gt; true)
      end
  
    end
  </code></pre>
  
  <p>When I tried to display my view selecting the first radio button (purpose = '1') I got the following error:</p>
  
  <pre><code>undefined method `radio_button_tag' for #&lt;PagesController:0x007f94d05c5e88&gt;
  </code></pre>
  
  <p>The line where it got the error was the one where params[:purpose] == 1 showing that purpose is populated properly.</p>
  
  <p>Other examples about checking the radio button were used with form_for where databases were updated.</p>
  
  <p>Any help would be appreciated.  I will keep looking.</p>
  
  <p>UPDATE: 4/4/3012 11:15 am CST</p>
  
  <p>My radio button is being checked now.  I moved the corrected logic to the helper as suggested by Mischa.</p>
  
  <p>Here is my helper code:</p>
  
  <pre><code>  def check_radio_button (purpose)
  
      if params[:purpose].blank?
        radio_button_tag(:purpose, purpose)
      elsif purpose == params[:purpose]
        radio_button_tag(:purpose, purpose, :checked =&gt; true)
      else
        radio_button_tag(:purpose, purpose)
      end
  
    end
  </code></pre>
  
  <p>Here is my view code that I rewrote as suggested by Catfish:</p>
  
  <pre><code>    &lt;%= check_radio_button("1") %&gt;&lt;%= label_tag :purpose_feedback, 'Suggestions' %&gt; 
      &lt;%= check_radio_button("2") %&gt;&lt;%= label_tag :purpose_prayer, 'Prayer Request' %&gt;
      &lt;%= check_radio_button("3") %&gt;&lt;%= label_tag :purpose_praise, 'Testimony' %&gt;
      &lt;%= check_radio_button("4") %&gt;&lt;%= label_tag :purpose_bug, 'Defects/Bugs' %&gt;
      &lt;%= check_radio_button("5") %&gt;&lt;%= label_tag :purpose_other, 'Other' %&gt;
  </code></pre>