LoginSignup
1
1

More than 1 year has passed since last update.

Railsチュートリアル(6版)の13章で_form.html.erbを使っていた場合の戸惑い解消

Posted at

はじめに

プログラミング初学者の@kat_logと申します。

Railsチュートリアル(Rails6版)の13章「13.3.2マイクロポストを作成する」の「リスト 13.43、13.44」で戸惑った部分について共有です。

戸惑ったポイント

10章の「10.1.1編集フォーム」の演習2をやった場合、new.html.erbedit.html.erbの一部を_form.html.erbとしてパーシャルにするため、テキストと違いが出て、どの部分をf.objectにするか戸惑いました。

結論

new.html.erbedit.html.erbは書き換え不要で、_form.html.erbを1箇所変えるとOKです。

_form.html.erb
<%= form_with(model: @user, local: true) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>

  <%= f.label :name %>
  <%= f.text_field :name, class: 'form-control' %>

  <%= f.label :email %>
  <%= f.email_field :email, class: 'form-control' %>

  <%= f.label :password %>
  <%= f.password_field :password, class: 'form-control' %>

  <%= f.label :password_confirmation %>
  <%= f.password_field :password_confirmation, class: 'form-control' %>

  <%= f.submit yield(:button_text), class: "btn btn-primary" %>
<% end %>

おわりに

お読みいただきありがとうございました。
自分と同じくrailsチュートリアル学習中の方の参考になれば嬉しいです。
一緒にプログラミング学習頑張っていきましょう〜😄

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