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="row">
<div class="col-sm-1">
<%= f.label :paid? %>
</div>
<div class="col-sm-1">
<%= f.check_box :paid, value: 'false' %>
</div>
</div>
To keep the i18n using of label
, you can use t
:
<%= f.label :my_field do %>
<%= f.check_box :my_field %> <%= t 'activerecord.attributes.my_model.my_field' %>
<% end %>
I would wrap the check box inside the label.
<%= f.label :terms_of_service do %>
<%= f.check_box :terms_of_service %>
I agree to the <%= link_to 'Terms of Service', policies_path %>
<% end %>
When the input field is wrapped by it's label, you actually don't need the for attribute on the label. The label will activate the check box without it on click. So even simpler:
<label>
<%= f.check_box :terms_of_service %>
I agree to the <%= link_to 'Terms of Service', policies_path %>
</label>
Generically for Rails, this could be a way to go about it (human_attribute_name works with i18n):
<label>
<%= f.check_box :terms_of_service %>
<%= User.human_attribute_name(:terms_of_service) %>
</label>
<div class="form-inline">
<%= f.check_box :subscribed, class: 'form-control' %>
<%= f.label :subscribed, "Subscribe" %>
</div>
Do you use bootstrap
?
Easy way to do is add :class => "span1"
in f.submit. I'm sure it worked!
On Rails 5.2, ruby 2.4 and bootstrap 4.1.1:
<%= form.check_box :terms_of_service, label: "your custom label...."%>
worked for me without having to indicate inline checkbox.