1

I was wondering if there is a way to use radio button to set the value of a field that is a text box.

    <%= radio_button("demographics_questionaires", "gender", "Male")%> Male <br\>
    <%= radio_button("demographics_questionaires", "gender", "Female")%> Female <<br\>
    <%= radio_button("demographics_questionaires", "gender", "Other")%> 
    Other <%= f.text_field gender %> 

Above code is incorrect, but something on that lines.

If the other radio button is selected, I want gender to be set to the value of the text_field? Not sure how you do that association?

Ex.

[radio button] Male

[radio button] Female

[radio button] Other __________________

where ____________ is the text box to enter.

In the end If the user choose male, I want the value the is stored in the database to be "Male", for female "FEMALE", or for other what the user inputs.

I kind of wanted to handle all the logic in the view. Is that possible or does it have to be done in the controller?

Any advice appreciated,

3
  • 2
    What are you expecting for "Other" gender? Commented Sep 11, 2010 at 3:45
  • he/she ? haa transgender Commented Sep 11, 2010 at 5:55
  • @NullUserException, @user391465 : See here : sarahmei.com/blog/2010/11/26/disalienation Commented Dec 27, 2010 at 0:37

1 Answer 1

1

Check this code. It may help you

<%= form_for @user do |f| %>
  <%= f.radio_button :email, 'male' %> male<br />
  <%= f.radio_button :email, 'female' %> Female<br />
  <%= f.radio_button :email, 'others' %> others<br />
  <%= f.hidden_field :email  %>
<% end %>

jquery script

<script type="text/javascript">

$("input[type='radio']").change(function(){ 
    if ($('input[type="radio"]:checked').val() == "others")
    {
        $('input[type="hidden"]').val('')   
        $('input[type="hidden"]').prop("type", "text");
    }
    else if ($('input[type="radio"]:checked').val() != "others")
    {
        $('input[type="hidden"]').prop("type", "hidden");
        $('input[type="hidden"]').val($('input[type="radio"]:checked').val())
    } 
});

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.