Rails - Cannot disable a button with an option box - ruby-on-rails

I have this:
<%= form_tag do %>
<%= label_tag :name, 'Name: ' %><%= text_field_tag 'name' %>
<%= submit_tag 'submit', disabled: #op %>
<%= label_tag :dis_false, "True" %>
<%= radio_button_tag :dis, :true %><br />
<%= label_tag :dis_true, "False" %>
<%= radio_button_tag :dis, :false %><br />
<% end %>
Routes are set up properly and #op = params[:dis].
Now, when I try to select "false" and click "submit", the button becomes disabled. This should only happen if the optin-box "True" is selected. So I was wondering, what's going on here? I was basically trying to make a simple app that would help me enable/disable a button via an option box.

I think I get your answer, when you send false #op is set to "false" not false.
Try this:
#op = params[:dis] == "true"
This is:
params[:dis] # comes as a string "true" or "false", since these are values radio button returns.
params[:dis] == "true" # is true without quotes when radio button with value "true"
# false, without quotes, otherwise, for example when
# when params[:dis] is "false"

It seems 'dis' would be better suited as a checkbox in this case. Disable/enable can also be handled via javascript / coffeescript. Add this to your js.coffee file in app/assets:
jQuery ->
$("#dis").change ->
$("#submit").attr "disabled", $("#dis").is(":checked")
Or in plain jQuery:
$(function() {
$("#dis").change(function() {
$("#submit").attr("disabled", $("#dis").is(":checked"));
});
});
And update your form fields:
<%= submit_tag 'submit', disabled: #op, :id => "submit" %>
<%= check_box_tag(:dis, '', false) %>

Related

checkbox in rails view updating database value without Javascript

I am trying to make the following:
Any solution to update the database with the value and with the checkbox without using JavaScript with a click on or click off?
<%= form_for :act_as do |f| %>
<%= f.check_box(:suspend_flag, { checked: (ActAs.find_by(ModifiedID:#misc_data[:user]).SuspendFlag ? false :true), class: 'abcd' }, 0, 1) %>
<%= f.submit %>
<% end %>

Retain checkbox value on page reload in form_with in Rails 5

I have a form created using form_with. What I need is to retain the values submitted using that form after page reload. I am able to save the value of text_field but not the value of check_box. What should I change in my code so that I can achieve the same?
html.erb
<%= form_with url: search_path,
id: :search_by_filter,
method: :get, local: true do |f| %>
<div>
<p><strong>Search by Name</strong></p>
<%= f.label 'Name' %>
<%= f.text_field :name, value: params[:name] %>
</div>
<br>
<div>
<%= label_tag do %>
<%= f.check_box :only_students, checked: params[:only_students] %>
Show only students
<% end %>
</div>
<br/>
<div class="submit_button">
<%= f.submit :Search %>
</div>
<% end %>
controller.rb
def get_desired_people(params)
people = Person.includes(:country, :state, :university).order(id: :desc)
people = people.where(is_student: params[:only_students]) if params[:only_students]
people = people.where(name: params[:name]) if params[:name].present?
people
end
Here I am able to retain the value of params[:name] but not the value of params[:only_students]. It always remains unchecked after form submission. How can I retain the checked and unchecked value?
f.check_box check_box_tag is expecting checked to by boolean value, and every param is a string (string is always evaluated to true if exists) so you should do:
checked: params[:only_students].present?
you don't have to worry about a value of param, as unchecked params are not send while posting.
EDIT:
above works for check_box_tag.
f.check_box is tricky, you should carefully read description: https://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-check_box
The behaviour you described seems pretty correct, you can deal with it or switch to check_box_tag as a better option when not updating model attributes
All the solutions above did not work for me. Try this:
<%= check_box_tag :only_students, true, params[:only_students] %>

Rails require radio button from a collection

I need a way for my form to not be sent if the user didn't bother to select any radio buttons.
I'd like to to that within the view and the controller, not in the model (the data shouldn't even be sent)
<%= form_tag("/bookings/new", method: "get") do %>
<% #flights.each do |flight| %>
<%= radio_button_tag :flight_id, flight.id %>
<% end %>
<%= submit_tag "book now" %>
<% end %>
edit, to clarify
normally I'd do
<%= f.text_field :name, required: true %>
but, as I have many radio buttons and I only need one for the form to work, I don't know how to implement it
You can set validation in the model to see the presence of checkbox if javascript is disabled. This is a more robust method.
validates :flight_id, :acceptance => true
Docs here - http://guides.rubyonrails.org/active_record_validations.html#acceptance
Edit
function validateCheckBox() {
var x = document.getElementById("flight_id").checked;
if(!x) {alert("Not checked")}
}
<%= submit_tag "book now" , :onclick => "validateCheckBox();" %>
<%= f.text_field :name, required: true %>
This still works perfectly for radio buttons, and it's okay if it ends up on all radio items. The form will still only require one input.
I just tested it on my Rails 6 app.

How do I make a textfield uneditable?

I have this textfield that I want to always have this value:
<%= text_field_tag :quantity, "1", class: "uneditable-input" %>
i.e. I don't want the user to be able to change the quantity value to anything other than 1.
I tried adding disabled: true and that worked to grey out the field, but it also disabled it - changing the behavior of the form (i.e. the form was submitted without a quantity value).
All I want to do is to force every person that submits this form to be able to see the Quantity of 1 - and not be able to change it - and have the system process quantity of 1.
How do I do that?
Try this <%= text_field_tag :quantity, "1", class: "uneditable-input", :readonly => true %>
or if you want to disable it you can do it this way
<%= text_field_tag :quantity, "1", class: "uneditable-input", :disabled => true %>
Just try:
<%= text_field_tag :quantity,:readonly => true%>
You can simply set the text field value to 1 server side, leaving disabled: true client side.
Try:
<%= text_field_tag :quantity, nil, {value: "1", disabled: true} %>
Output:
<input disabled="disabled" id="quantity" name="quantity" type="text" value="1">

Ruby on Rails 3 - How Can I Check a radio_button_tag In My Controller?

I have a radio button on a contact form where someone can select 1 of 5 values. I do not have a default value for the radio button. I am using form_tag since this data will not be stored on a database.
Here is my code for the radio button:
<%= label_tag "Purpose:" %>
<%= radio_button_tag :purpose, '1' %><%= label_tag :purpose_feedback, 'Suggestions' %>
<%= radio_button_tag :purpose, '2' %><%= label_tag :purpose_prayer, 'Prayer Request' %>
<%= radio_button_tag :purpose, '3' %><%= label_tag :purpose_praise, 'Testimony' %>
<%= radio_button_tag :purpose, '4' %><%= label_tag :purpose_bug, 'Defects/Bugs' %>
<%= radio_button_tag :purpose, '5' %><%= label_tag :purpose_other, 'Other' %>
I have the radio button with six text fields on my form. I do error checking field by field in my controller, starting with the radio button which is at the top of the form. The error checking is checking all my fields correctly. However if I select one of the radio button values for params[:purpose] the value is populated properly but the radio button is not checked when the view is displayed. If I select purpose and enter values for three text fields for example, the values of the text fields are still on the form but the radio button is unchecked even though purpose has a value.
I found this link How to set the value of radio button after submitting form? and developed the following code in my controller:
def check_radio_button
case params[:purpose]
when '1'
radio_button_tag(:purpose, '1', :checked => true)
when '2'
radio_button_tag(:purpose, '2', :checked => true)
when '3'
radio_button_tag(:purpose, '3', :checked => true)
when '4'
radio_button_tag(:purpose, '4', :checked => true)
when '5'
radio_button_tag(:purpose, '5', :checked => true)
end
end
When I tried to display my view selecting the first radio button (purpose = '1') I got the following error:
undefined method `radio_button_tag' for #<PagesController:0x007f94d05c5e88>
The line where it got the error was the one where params[:purpose] == 1 showing that purpose is populated properly.
Other examples about checking the radio button were used with form_for where databases were updated.
Any help would be appreciated. I will keep looking.
UPDATE: 4/4/3012 11:15 am CST
My radio button is being checked now. I moved the corrected logic to the helper as suggested by Mischa.
Here is my helper code:
def check_radio_button (purpose)
if params[:purpose].blank?
radio_button_tag(:purpose, purpose)
elsif purpose == params[:purpose]
radio_button_tag(:purpose, purpose, :checked => true)
else
radio_button_tag(:purpose, purpose)
end
end
Here is my view code that I rewrote as suggested by Catfish:
<%= check_radio_button("1") %><%= label_tag :purpose_feedback, 'Suggestions' %>
<%= check_radio_button("2") %><%= label_tag :purpose_prayer, 'Prayer Request' %>
<%= check_radio_button("3") %><%= label_tag :purpose_praise, 'Testimony' %>
<%= check_radio_button("4") %><%= label_tag :purpose_bug, 'Defects/Bugs' %>
<%= check_radio_button("5") %><%= label_tag :purpose_other, 'Other' %>
Your problem is that radio_button_tag is not a function to be used in your controller. The link that you posted has radio_button_tag in a helper.
Re-read the question that you posted and try it again.
Basically you create a helper method that renders a check_box_tag and then you call that helper method in your view:
Example helper
def my_check_box_tag(my_param) do
....
end
In my view
<%= my_check_box_tag("test") %>

Resources