1
votes

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,

1
What are you expecting for "Other" gender?NullUserException
he/she ? haa transgenderdarewreck
@NullUserException, @user391465 : See here : sarahmei.com/blog/2010/11/26/disalienationZabba

1 Answers

0
votes

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())
    } 
});