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

后端 未结 14 1438
天涯浪人
天涯浪人 2021-02-03 17:34

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:

<
相关标签:
14条回答
  • 2021-02-03 18:09
    <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>
    
    0 讨论(0)
  • 2021-02-03 18:12

    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 %>
    
    0 讨论(0)
  • 2021-02-03 18:12

    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>
    
    0 讨论(0)
  • 2021-02-03 18:14
      <div class="form-inline">
        <%= f.check_box :subscribed, class: 'form-control' %>
        <%= f.label :subscribed, "Subscribe" %>
      </div>
    
    0 讨论(0)
  • 2021-02-03 18:14

    Do you use bootstrap? Easy way to do is add :class => "span1" in f.submit. I'm sure it worked!

    0 讨论(0)
  • 2021-02-03 18:15

    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.

    0 讨论(0)
提交回复
热议问题