Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep a checkbox and label on the same line in a Rails form?

What should I do to keep the label of a checkbox on the same line with the checkbox in a rails view containing a form?

Currently the label goes on the next line:

<div class="span6 offset3"> <%= form_for(@user) do |f| %> <%= render 'shared/error_messages', object: f.object %>    <%= f.label :name %>   <%= f.text_field :name %>    <br>   <%= f.check_box :terms_of_service %>   <%= f.label :terms_of_service, "I agree to the #{link_to 'Terms of Service', policies_path}.".html_safe %>   <br><br>    <%= f.submit "Create my account", :class => "btn btn-large btn-primary" %> <% end %> 

Thank you, Alexandra

like image 599
AndraD Avatar asked Sep 19 '12 03:09

AndraD


People also ask

How to align checkboxes and their labels with each other?

Method 1: By making the position of checkbox relative, set the vertical-align to the middle can align the checkboxes and their labels. Here, we have made the position of the checkbox relative to the label.

How do I add a checkbox to a form?

Create <form> and <div> elements. Place “checkbox” input type in the <label> element. <form> <div> <label> <input type="checkbox" /> Label text </label> </div> </form>

How do I create a checkbox with a clickable label?

This means that the checkbox gets on/off when clicking the label. In this snippet, we suggest two methods of creating a checkbox with a clickable label. This is possible to do either by wrapping a <label> tag, or by using a “for” attribute. First, let’s see how to use the method of wrapping the <label> tag.

What is a checkbox and what is it for?

The checkbox is one of the HTML forms that is used on every website. How to align the checkbox and its label? This is a question that developers frequently ask.


2 Answers

According to the bootstrap wiki, it must be

<label class="checkbox">   <input type="checkbox"> Check me out </label> 

which in Ruby on Rails is

<%= f.label :terms_of_service do %>   <%= f.check_box :terms_of_service %>   I agree to the <%= link_to 'Terms of Service', policies_path %>. <% end %> 
like image 168
Benoît Legat Avatar answered Sep 23 '22 05:09

Benoît Legat


It looks like you're using Bootstrap, so I recommend adapting your view code to use the horizontal form layout described in this section of the Bootstrap docs: https://getbootstrap.com/docs/4.3/components/forms/#horizontal-form

like image 44
David Underwood Avatar answered Sep 25 '22 05:09

David Underwood