How to add a switch toggle button to simple form in rails

主宰稳场 提交于 2019-12-23 09:25:10

问题


I'm using Rails 4 and Simple Form with Bootstrap.

I want that my checkbox will not like that:

<%= c.input :my_bool_field, label: false, class: "form-control" %>

but something like that (I have the CSS for that)

<label class="switch switch-primary">
    <input type="checkbox" />
    <span></span>
</label>

I know I can use simple_form wrappers, but I find them a little bit confusing.. Can someone help me up creating the checkbox with that styling?


回答1:


I found this project very useful and complete. It is based on twitter bootstrap: http://www.bootstraptoggle.com/

There is a rails gem that embedded it :




回答2:


Could you go to this link and check it out "Switches". getbootstrap.com

I hope this example serves you, I'm currently use Bootstrap 4.3 and Rails 5.2;

Use a "custom-switch" in the form of your Model and its going to work:

-- model/_form.html.erb

<%= form_with(model: employee, local: true) do |form| %>
 <div class="custom-control custom-switch">
    <%= form.check_box :active, class: "custom-control-input", id: "customSwitch1"   %>
  <label class="custom-control-label" for="customSwitch1">Status: </label>
 </div>
 <div class="actions">
     <%= form.submit %>
 </div>
<% end %>

I hope helps you, I'm a newbie in Rails and Bootstrap.

Regards!




回答3:


just put checkbox inside

<label class="switch switch-primary">
     <%= f.input_field :my_bool_field, label: false, as: :boolean, class: 'form-control' %>
    <span></span>
</label>



回答4:


Checkout the Rails Switchery gem found at https://github.com/abpetkov/switchery

Easy customization for bootstrap and ios style switches without trying to change simpleforms pre-built wrappers via the initializer or calling jquery functions in the view.



来源:https://stackoverflow.com/questions/39594248/how-to-add-a-switch-toggle-button-to-simple-form-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!