1
0

More than 3 years have passed since last update.

Ruby on Railsのform_withでのインスタンス変数(model)とname属性の関係

Last updated at Posted at 2021-05-16

※プログラミング学習中の私がアップロードしてます。理解が浅いです。
この記事は厳密な仕様に関するものではなく、考え方理解のまとめとして受け取ってください。

Ruby on Railsのform_withとは
form_withとは、Railでフォームタグを簡単に作成するためのヘルパーです。使い方をマスターすれば、コーディングがずっと楽になります。RailsでWebアプリケーションを作成する際に、ほぼ必須のヘルパーです。
input 要素 では, フォームのデータが送信されるときに,name 属性 の値が付加されており,対象とする要素のデータに簡単にアクセスできます。

<%= form_with model: @user do |f| %>
  <%= f.label :name, '名前' %>
  <%= f.text_field :name %>
  <%= f.submit "送信" %>
<% end %>
生成タグ
<form action="/users" accept-charset="UTF-8" data-remote="true" method="post">
  <label for="user_name">名前</label>
  <input type="text" name="user[name]" id="user_name" />
  <input type="submit" name="commit" value="送信" data-disable-with="送信" />
</form>

name=>user_name
name=>user[name]

モデル名のuserが追加されます

1
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
1
0