1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

form_withの書き方(情報を保存しない時・情報をDBに保存する時)

Posted at

form_tagのように、入力された情報を保存しない時の書き方

sample.html
<%= form_with url: root_path do |form| %>
  フォーム内容
<% end %>

form_forのように、入力された情報データベースに保存する時の書き方

sample.html
<%= form_with model: モデルクラスのインスタンス do |form| %>
  フォーム内容
<% end %>

データーベースに保存する場合、form_withの引数には モデルクラスのインスタンス を指定する。
モデルクラスのインスタンスとは 保存したいテーブルのクラスのインスタンス のことです。

例)

sample.html
<%= form_with model: @user do |form| %>
  <%= form.text_field :name %>
  <%= form.submit %>
<% end %>
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?