1
0

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 3 years have passed since last update.

【rails】衝撃!form_with では、value が要らない。

Posted at

初期値の設定にvalue を書かなくて良い!

rails の form_withにて、value を書かなくて良いことをさっき知ったので、記録を残しておこうと思います。

記述例

  <%= form_with(model: @user, local: true) do |form| %>
    <h2 class="text-center mt-5">ユーザ情報修正</h2>

    <table width="60%" style="margin: auto;">
      <tr height="40">
        <td align="center">氏名</td>
        <td>
          <%= form.label :last_name, "姓" %>
          <%= form.text_field :last_name %>
          <%= form.label :first_name, "名" %>
          <%= form.text_field :first_name %>
        </td>
      </tr>
  <% end %>

これで画面表示すると、初期値が表示されました。

form_with 内の、model: @user属性。
ここで、@user に中身が入っていると、form_value は空気を読んで、初期値を入れ込んでくれます。

便利!

情報更新画面とか作るときに、もうvalue を設定する必要ありません。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?