I have a code of checkbox below in ruby on rails:
<%= check_box(:Monday,{:id => "Monday",:value => "Monday"}) %>
But, it shows only the checkbox but not shows its text i.e "Monday".
So what should I do to display the text of checkbox, kindly suggest me, waiting for reply. Thanks
Have you read this?
http://apidock.com/rails/v4.0.2/ActionView/Helpers/FormTagHelper/check_box_tag
Either you use the check_box_tag or the f.check_box inside a form builder, plus you have to add a label to display it.
The construct you are using just generate the <input type="checkbox" value="something" /> and not the label, that you have to add, just like text or with a <%= label_tag 'whatever' %>.
try the below code
<%= check_box :monday, {:class => "anyclass", :style => "anystyle"}, "monday" %>
one way to do is
<%= check_box_tag "Monday", 'yes', id:"monday" %> Monday
or u can do this also
<%= check_box_tag "Monday", "yes" %>
<%= label_tag "Monday" %>
You need to use a label:
<%= form_tag your_path do %>
<%= label "Day" %>
<%= check_box "Monday", "yes" %>
<% end %>
Related
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] %>
I have this code and I want to make it display with checkboxes instead of the dropdown menu. Can that be achieved? .
<%= f.select :job_type_cont,[['Full Time','Full Time'],['Part Time','Part Time'],['Permanent','Permanent']],{:include_blank => 'All....'},class:"form-control",id:"bed_room" %>
For that is neccesary a checkbox input
First you need in you controller receive you parameter like an array:
params.require(:model).permit(:others, job_type_cont: [])
And later, you should do an each, like
<% job_type_conts = ['Full Time', 'Part Time', 'Permanent']%>
<% job_type_conts.each do |type| %>
# Edit: Check id and for of label and checkbox input
<%= f.check_box( :job_type_cont, { multiple: true, id: "#{type.gsub(" ","-")}"}, type ) %>
<%= f.label :job_type_cont, type, for: "#{type.gsub(" ","-")}" %>
<% end %>
You can watch a good simple guide
Rails multiple checkboxes of Sophie Déziel.
I have a field called delivery_day which is of type string in delivery_preference model.
In form, I want to provide 7 checkboxes for each day like Sunday,Monday,etc., and later want to concat.
For example if a user checks Sunday and Friday, I want to concat & store it as "Sunday,Friday" in delivery_day field.
Thanks in Advance!!
You can design your form like this -
<%= form_for #delivery_preference do |f|%>
<%= f.check_box :delivery_day, {multiple: true}, "Sunday" %>Sunday
<%= f.check_box :delivery_day, {multiple: true}, "Monday" %> Monday
<%= f.submit "Add" %>
<% end %>
After submitting the form, you can get your check box selections in your controller as follows:
def your_action_name
params[:delivery_preference][:delivery_day].delete("0")
DeliveryPreference.create(delivery_day: params[:delivery_preference][:delivery_day].join(","))
end
Hope it helps!
Might have better solutions, but when I encountered similar problem, I used check_box_tag to solve it.
<%= check_box_tag "delivery_preference[delivery_day][0]", 'monday' %>Monday
<%= check_box_tag "delivery_preference[delivery_day][1]", 'tuesday' %>Tuesday
<%= check_box_tag "delivery_preference[delivery_day][2]", 'wednesday' %>Wednesday
<%= check_box_tag "delivery_preference[delivery_day][3]", 'thursday' %>Thursday
<%= check_box_tag "delivery_preference[delivery_day][4]", 'friday' %>Friday
<%= check_box_tag "delivery_preference[delivery_day][5]", 'saturday' %>Saturday
<%= check_box_tag "delivery_preference[delivery_day][6]", 'sunday' %>Sunday
then you will receive an array like { deliver_day: ['monday', 'tuesday'] } in you controller. You can choose to concat in your controller, and then save, or you can move the logic to your model.
in your controller, you strong parameter should be like
params.require(:delivery_preference).permit(.., :deliver_day => [])
to permit the array.
I do not have enough reputation to leave a brief comment yet. However, does your migration have delivery_day have something similar to t.boolean :public, default: true_or_false_here within it?
If so, within the form, you could have something like:
...
<div class="form-group">
<%= f.label :public, class: 'checkbox' do %>
<%= f.check_box :public %> Monday
<% end %>
</div>
<div class="form-group">
<%= f.label :public, class: 'checkbox' do %>
<%= f.check_box :public %> Tuesday
<% end %>
</div>
...
After above, you could designate (via boolean logic) your "concat & store it as "Sunday,Friday"
I have a boolean value on a task model I want to show the value in a checkbox without the possiblity of changing the checkbox value.
How can I achieve this? Why doesn't my code work?
<%= form_for(#task) do |f| %>
<%= f.check_box :important, {}, disabled="disabled" %>
<% end %>
Thanks in advance.
use check_box_tag with disabled: true.
I finally found an easy solution. The check_box_tag or check_box just confused me and I could not call the boolean value related on the checkbox (checked = true, unckecked = false).
This works perfectly:
<%= form_for(#task) do |f| %>
<%= f.check_box :important, :disabled => true %>
<% end %>
I have:
<% #count=0%>
<%addr.each do |addr|%>
<% #count+=1%>
<%if addr==''%>
<%= #count%>
<%= form_for(:hotelUser,:url=>{:controller=>'HotelUsers',:action=>'createAddress'}) do |hotelUser|%>
<%= hotelUser.text_area(:address,:cols=>67,:rows=>3)%>
<p style="text-align: center;width: 50%;">
BACK<%= submit_tag("PROCEED TO PAYMENT",{:action=>'show'})%>
</p>
<%end%>
<% break%>
<%end%>
<%end%>
I want to concatenate the value of #count with text_area id :address. For example if #count=4 then i will get :address4. Please help me solve this problem?
Do this way
<%= hotelUser.text_area(:address, :cols => 67, :rows => 3, :id => "address"+#count.to_s)%>
Done!
This should work.
<%= hotelUser.text_area(:address,:cols=>67,:rows=>3, :id => 'address<%=#count%>')%>
If your table has fields address1, address2, ... you need to reference that field in the first parameter of the text_area helper. So the code would be:
<%= hotelUser.text_area("address#{#count}",:cols=>67,:rows=>3)%>