LoginSignup
0
1

More than 3 years have passed since last update.

deviseでログイン機能を実装したものの、データベースへ書き込めない問題

Posted at

起きた問題

deviseにてユーザーのログイン機能やユーザー編集機能などを実装したが、ユーザー編集機能のページedit.html.erbにて入力した内容がうまくデータベースに反映されない。

具体的にいうとnameのカラムだけなぜかdbに反映されなかったので、調べてみた。

schema.rbを見てもちゃんとカラムが追加されている。。。

解決した方法とソース

まず、解決した方法は簡単で、application_controller.rbに以下のコードを追加するだけ。

application_controller.rb
class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    # 「登録時(sign_up)」に許可するパラメータを追加
    devise_parameter_sanitizer.permit(:sign_up, keys: [:name])

    # 「更新時(account_update)」に許可するパラメータを追加
    devise_parameter_sanitizer.permit(:account_update, keys: [:name])
  end
end

deviseではどうやらデフォルトでemail、password以外は許可しない設定になっているらしく、上記のようなコードを追加することで許可してあげないといけないようだった。

以下のURLはdeviseのGithub READMEから。

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