LoginSignup
0
0

More than 1 year has passed since last update.

deviseで用意されている form_forをform_withに変更する

Posted at

はじめに

Railsのgemにあるdeviseを使用するとユーザー登録のためのヘルパーメソッドはform_forになっているかと思います。

Rails5.1になって以降、form_forからform_withに推奨されていますので変更が必要かと思います。

app\views\devise\registrations\new.html.erbを元に置き換えをしていきます。

変更前

app\views\devise\registrations\new.html.erb
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>

変更後

app\views\devise\registrations\new.html.erb
<%= form_with model: @user, url: user_registration_path, id: 'new_user', class: 'new_user', local: true do |f| %>

model: @userでデータベースのusersテーブルに保存されているユーザー情報を入力フォームに入れてくれる構造になっています。

また、urlの指定には注意してください。
上記はregistrationsフォルダで行っていますが、他にも複数存在しますのでurl指定の際はお気をつけください。

参考にさせていただいた記事

「ユーザー認証機能 devise」
https://zenn.dev/odentravel/books/e69a157daeecb3/viewer/f54358

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