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


看起来您使用的是Bootstrap,因此我建议您调整视图代码以使用本节Bootstrap文档中描述的水平表单布局:http://getbootstrap.com/css/#forms


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 %>

 <br>
  <%= f.check_box :terms_of_service, :style => "float:left;" %>
  <%= f.label :terms_of_service, "I agree to the #{link_to 'Terms of Service', policies_path}.".html_safe, :style => "float:left;" %>
  <br>
链接地址: http://www.djcxy.com/p/41452.html

上一篇: 当用户点击显示链接时,显示密码,再次点击时将其隐藏

下一篇: 如何在Rails表单的同一行保留复选框和标签?