빠른 참조

SimpleForm 치트시트

Ruby on Rails의 SimpleForm gem 빠른 참조 가이드: 입력 헬퍼, 폼 래퍼 및 필드 옵션.

· 게시일

SimpleForm은 Ruby on Rails 애플리케이션을 위한 유연한 폼 헬퍼를 제공합니다.

기본 폼 템플릿

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

입력 유형 및 옵션

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

커스텀 폼 래퍼 (Wrappers)

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

폼 연관 관계 (Associations)

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

관련 주제