1

I fetched couple posts over here but I am still unable to make it work. When "Other" radio button is checked I would like a f.text_field to show up and when is unchecked to be hidden. I would like after that to be able to input a text value for thr attribute f.client_consent_refusal_reason.

Here is my HTML:

<table>
  <tr>
    <td>
      <%= form_for(@client, :url => {:controller => 'client', :action=> :outcome_consent_complete, :id => @client.id }) do |f| %>
        <%= f.radio_button :client_consent_refusal_reason, 'Hearing loss', :checked => false %>
        <%= f.label :radio_button_label, 'aaa' %>
        <br>
        <%= f.radio_button :client_consent_refusal_reason, 'bbb', :checked => false %>
        <%= f.label :radio_button_label, 'ccc' %>
        <br>
        <%= f.radio_button :client_consent_refusal_reason, 'ddd', :checked => false %>
        <%= f.label :radio_button_label, 'eee' %>
        <br>
        <%= f.radio_button :client_consent_refusal_reason, 'fff', :checked => false %>
        <%= f.label :radio_button_label, 'ggg' %>
        <br>
        <%= f.radio_button :client_consent_refusal_reason, 'Other', :checked => false %>
        <%= f.label :radio_button_label, 'Other' %>
        <%= f.text_field :client_consent_refusal_reason %>
        <br><br>
        <div class="actions">
          <%= f.submit 'Submit' %>
        </div>
      <% end %>
    </td>
  </tr>
</table>

When any of the above radio buttons are checked the field client_consent_refusal_reason should be set to the associated value, but when Other is checked, I would like to be able to capture the correspondent value.

I don't know how to solve this problem.

1 Answer 1

1

I replaced this part of the code:

<%= f.radio_button :client_consent_refusal_reason, 'Other', :checked => false %>
<%= f.label :radio_button_label, 'Other' %>
<%= f.text_field :client_consent_refusal_reason %>

with this:

<%= f.radio_button :client_consent_refusal_reason, 'Other', :checked => false,
    :onchange => "$('other_reason_input')[this.checked ? 'show' : 'hide']();" %>
<%= f.label :radio_button_label, 'Other (please list): ' %>

<div id="other_reason_input" style="display:none;">
  <%= f.text_field :client_consent_refusal_reason_other %>
</div>

And in the model class I had to add these lines:

  attr_accessor :client_consent_refusal_reason_other

  def client_consent_refusal_reason_other=(value)
    self.client_consent_refusal_reason = value if client_consent_refusal_reason == 'Other'
  end

  def client_consent_refusal_reason_other
    client_consent_refusal_reason
  end

Now everything is working as expected.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.