1

I am new to RoR. I want to know how do we make a radio button default selected just first time while creating new record?

I tried

<%= radio_button_tag 'scheduler', 'now', true%> Now <br>
<%= radio_button_tag 'scheduler', 'future'%> Future<br> 

It makes the first radio button (having value Now) checked but when i select second radio button (Future) and leave other mandatory fields blank on the form then again the first radio become selected instead of second.

How to resolve this.

Note: the field "scheduler" is just a virtual field on this form that will display another HTML containing a datetimepicker if "Future" option is selected and that datetimepicker field "schedule_date" is the actual field of the model. If option "Now" is posted then simply it will save the current date and time into the database table for that field "schedule_date". So i just have to show/hide an HTML by using these radio buttons.

3 Answers 3

2

When you say 'leave the mandatory fields blank' i guess you are submitting the form and getting the edit form back from the server with a warning flash? What I think you need to do here is instead of setting the Now field to true, you need to use a variable and set it to true on when creating a new form. Then when you submit the form to the server the controller's create method should look at the params hash, set the variable, and if the submission fails validation is should return the edit form with the Now variable set to false and the Future variable set to true

so in the create method of the controller you have something like

@now = params[:scheduler][:now]

in the new method of the controller you have:

@now = true

and in the view:

    <%= radio_button_tag 'scheduler', 'now', @now%> Now <br>
<%= radio_button_tag 'scheduler', 'future', !@now %> Future<br> 
Sign up to request clarification or add additional context in comments.

1 Comment

Ok, will this code make "Now" option by default selected first time only?
0

This is true because you have set the first radio button to be selected by default. So when you select the second radio button and then refresh/submit the form, the page comes to its original position losing the previous selected second button.

This is just like when you enter the form fields and then refresh the form or come back again to the form after going back, you loss the data that you filled previously.

Solution:

I say get the checked radio button value and set it using jquery as-

    $('input[name="myradios"]').change(function() {
             alert($(this).val());
    });

2 Comments

I got same problem, but what is the best idea for this?
I don't want to use JQuery for this.
0

Try:

<%= radio_button_tag 'scheduler', 'now', true %> Now <br>
<%= radio_button_tag 'scheduler', 'future', params[:scheduler].eql?("future") %> Future<br> 

2 Comments

thanks, Can i make this field "scheduler" as a virtual field of my Model?
Your code is working fine but i saw that when i select the option "Future" then it becomes selected but also the property of Now is set as "checked = 'checked'" because we have given hardcoded true value for this and that is not correct.

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.