クイックリファレンス

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

カスタムフォームラッパー

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

関連テーマ