Prerequisites: run component setup (view_primitives:install) once per app.
Toggle switch built from a styled checkbox. Thumb slides on check; no JavaScript required.
rails g view_primitives:add switchCreates app/components/ui/switch_component.rb.
Refresh after a gem upgrade:
rails g view_primitives:update --only switch<%# Standalone %>
<%= ui :switch, name: "notifications", id: "notifications" %>
<%# With label %>
<%= ui :switch, name: "dark_mode", id: "dark_mode", label: "Dark mode" %><%= ui :switch, name: "emails", id: "emails", label: "Email notifications", checked: true %><%= form_with model: @settings do |f| %>
<%= ui :switch, name: "settings[dark_mode]", id: "settings_dark_mode",
label: "Dark mode", checked: @settings.dark_mode %>
<% end %>| Option | Type | Default | Description |
|---|---|---|---|
label |
String | nil |
Visible label rendered beside the switch |
checked |
Boolean | false |
Initial on/off state |
**html_attrs |
Hash | — | Forwarded to the hidden <input type="checkbox"> |