Kurzreferenz

SimpleForm Spickzettel

Schnellreferenz für das SimpleForm-Gem in Ruby on Rails: Eingabe-Helper, Formular-Wrapper und Feldoptionen.

· Veröffentlicht

SimpleForm bietet flexible Formular-Helper für Ruby on Rails-Anwendungen.

Grundlegendes Formular-Template

<%= simple_form_for @user do |f| %>
  <%= f.input :email, required: true, autofocus: true %>
  <%= f.input :password, required: true %>
  <%= f.input :remember_me, as: :boolean %>
  <%= f.button :submit, "Sign in", class: "btn-primary" %>
<% end %>

Eingabetypen und Optionen

<%= f.input :bio, as: :text, input_html: { rows: 5 } %>
<%= f.input :role, collection: ['Admin', 'Editor', 'Subscriber'], prompt: "Select Role" %>
<%= f.input :birth_date, as: :date, start_year: 1980 %>
<%= f.input :accepted_terms, as: :boolean, label: "I accept the terms and conditions" %>

Benutzerdefinierte Formular-Wrapper

<%# Benutzerdefinierten Wrapper anwenden (definiert in config/initializers/simple_form.rb) %>
<%= simple_form_for @user, wrapper: :inline_form do |f| %>
  <%= f.input :query, label: false, placeholder: "Search..." %>
  <%= f.button :submit, "Search" %>
<% end %>

Formular-Assoziationen

<%# Belongs-to- / Has-many-Assoziationen %>
<%= f.association :company, collection: Company.all, prompt: "Choose Company" %>
<%= f.association :roles, as: :check_boxes %>

Verwandte Themen