Ruby On Rails: make radio button default selected first time - ruby-on-rails

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.

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>

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

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

Related

Determine which submit button clicked when they have same name

I have a form with multiple submit buttons called "select". I want to be able to tell which was clicked. I can't set the value to the number because I want them to say "select". How can I do this?
You can give them different name attributes instead of different value attributes. For example:
<%= f.submit "select", name: "select_one" %>
<%= f.submit "select", name: "select_two" %>
Clicking the first one will insert
"select_one"=>"select"
into your POST parameters and clicking the second one will insert
"select_two"=>"select"
into your POST parameters.
In your controller, simply use params[:select_one] and/or params[:select_two] as conditions in your logic, like so:
if params[:select_one]
puts "select_one was clicked"
elsif params[:select_two]
puts "select_two was clicked"
end

Rails select 'Other' option creating text field

Given a standard select code:
<%= f.select :type_name, [['Genomics','Genomics'],['Proteomics','Proteomics'],['Transcriptomics','Transcriptomics'],['Other','Other'] %>
Can someone explain how I would go about creating a text field when 'Other' is selected? So that the type_name can be something other than the options in the select?
I realise this is a simple question, but I haven't found a concise answer as of yet!
There are lots of ways to do this, but they all require JavaScript. The general approach I like is to put a hidden text field in the form, then attach a JavaScript event handler to the select tag that shows the field when the "Other" option is selected.
Here's a gist of the script I typically use for this. It handles the JavaScript binding using data attributes. Add the script to your assets, then put something like this in your form:
<%= f.select :type_name, [['Genomics','Genomics'],['Proteomics','Proteomics'],['Transcriptomics','Transcriptomics'],['Other','Other'] %>
<%= f.text_field :type_name_other, "data-depends-on" => "#object_type_name", "data-depends-on-value" => "Other" %>
where #object_type_name is the HTML id of your dropdown.
You need to create an attr_accessor on the model f is attached to (like type_name_other), add a text_field to the form below the select for type_name_other in a div that is initially hidden (in CSS: display:none), then create a javascript listener that detects when the select form has changed and if the selected ansser is "other" show the hidden field else hide it. You will then need to see if type_name_other has a value when processing the form and use it if so.

rails, unset value of checkboxes when none are selected

I dynamically generate a set of checkboxes in my view:
<% Event::NOTIFICATIONS.each do |notification| %>
<%= check_box_tag "event[notifications][]",
notification,
#event.notifications.try(:include?, notification) %>
<%= t("events.notifications.#{notification}") %>
<% end %>
The values are stored as an array.
However, when I edit an event and uncheck one of the notification boxes (so no notification boxes are left checked), it will not clear the array. The parameter hash does not contain the notification key either.
Any ideas on how to solve this?
check_box_tag doesn't work like check_box. check_box creates a hidden field which returns 0 as the default value for an unchecked checkbox. check_box_tag, on the other hand, doesn't return anything with the form if it is uncheked. An easy way to solve this is to add a hidden field with the same name as the checkbox but with a value of nil. This returns an array with a nil value as the first element which Rails should ignore.
hidden_field_tag 'event[notifications][]', nil

One select_tag and multiple button_to submit buttons

I have one select_tag and multiple button_to buttons on the same page. I'm looking to use the parameters from the select_tag for several of the buttons, is there a non-form way to do this?
The reason I don't want to use a form is two fold:
1. the number of buttons is dynamic
2. the positioning of the buttons do not follow the form structure (object at the top and submit at the bottom)
-here is one of the create methods that are linked to one of the buttons
def create
params[:options].each do |x|
#connector = Connector.find_or_create_by_options_id_and_follow_id(x.id, current_user.follow(#product).id)
#connector.save
end
end
I checked and this params[:options] is always nil no matter what is selected by me when I test
<%= select_tag :options, options_for_select(#current_user_options.map {|p| [p.name, p.id] }), {:multiple => true} %>
button_to method actually creates a form which has a submit button, with the form action being the url which you had mentioned in button_to. So when you hit the submit button, obviously the options param will be nil, since the button_to form has no data in it
You can use a form itself, and form does not mandate, having an object at the top and submit button below. Submit button just submits the form irrespective of its position in the form
It sounds like you actually need a second field in your form that's a select field or radio buttons to choose your action. Then you have one button that submits the form, and in your controller you can then parse that new action field and decide where to redirect the user.

RoR... my scaffold table doesn't fill when i use radio buttons

I have a problem, i made a scaffold, generating the table "requirements", i want the user to fill the fields of the table in the edit and the new requirement with select boxes and radio buttons. The select box and the radio buttons appear in the explorer but when i select one option or one button, that value selected it's not reflected in the db. The code im using its the next: (As you can see i used the original cicle f.label(:notif_card) and f.text_field(:notif_card) generated by the scaffold, but i deleted the last one and used the select box in this case.)
<%= f.label :notif_card %><br />
<% value = { "Good" => 0, "In Progress" => 1, "Denied" => 2 }%>
<%= select( #requirement, :notif_card , value) %>
<% if value == 1 %>
<% #requirement.notif_card = 1 %>
<%end%>
I just want to delete that text_field and replace it with a select box! Everything you can do i will appreciate it alot! If something needs to be in the model or in the controller besides the code that i'm using please let me know. Thanks for your help!
if you want to use the radio buttons for storing values from form to DB you can use the radio buttons..
the syntax will be like
<b>notif_card</b><br/>
<%= f.radio_button:notif_card,'0'%>Good<br\>
<%= f.radio_button:notif_card,'1'%>In progress<br\>
<%= f.radio_button:notif_card,'2'%>Denied<br\>
<%= f.submit %>
this will store values on the database by using radio buttons.
if you want to use check box method for the same, you can use but it is of Boolean method.so needs to be declared as boolean when start generating scaffolding it.

Resources