LoginSignup
0
0

More than 1 year has passed since last update.

Rails フォームヘルパー Cheat Sheet !

Posted at

フォーム作成時に構文を毎回調べているので備忘録兼ねてまとめました。

バージョン

下記のバージョンで確認
Rails version: 7.0.2.2

ActionView::Helpers::FormHelper

<%= form.color_field :favorite_color %>
<%= form.date_field :born_on %>
<%= form.datetime_field :user, :born_on, min: Date.today %>
<%= form.datetime_local_field :graduation_day %>
<%= form.email_field :address %>
<%= form.file_field :post, :image, multiple: true %>
<%= form.hidden_field :parent_id, value: "foo" %>
<%= form.month_field :birthday_month %>
<%= form.number_field :price, in: 1.0..20.0, step: 0.5 %>
<%= form.password_field :password %>
<%= form.phone_field :phone %>
<%= form.range_field :discount, in: 1..100 %>
<%= form.search_field :name %>
<%= form.telephone_field :phone #alias phone_field %>
<%= form.text_area :message, size: "70x5" %>
<%= form.text_field :post, :title, maxlength: 30, class: "title" %>
<%= form.time_field :started_at %>
<%= form.url_field :homepage %>
<%= form.week_field :birthday_week %>

actionview_helpers_formhelper.png

ActionView::Helpers::FormOptionsHelper

collectionは主にドロップダウンリストをDBからの情報を元に作成したいときに使用します。

<%= form.collection_select :city_id, City.order(:name), :id, :name %>
<%= form.collection_radio_buttons :city_id, City.order(:name), :id, :name %>
<%= form.collection_check_boxes :city_id, City.order(:name), :id, :name %>

参考

RUBYGUIDES: Action View Form Helpers
https://guides.rubyonrails.org/form_helpers.html

MDN: : 入力欄 (フォーム入力) 要素
https://developer.mozilla.org/ja/docs/Web/HTML/Element/input

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0