LoginSignup
20
17

More than 3 years have passed since last update.

[devise]パスワード入力不要のユーザー編集

Last updated at Posted at 2020-04-05

deviseを使用したユーザー情報の編集で、バスワード入力を省略する

手順 3つ
1. deviseコントローラファイルの作成(routes.rbの編集)
2. registrations_controller.rbの編集
3. edit.html.erbの編集

1.deviseコントローラファイルの作成(routes.rbの編集)

deviseのコントローラを編集するため、以下を行ってください。

ターミナル

$ rails g devise:controllers users

①コマンド実行で作成されるファイル
Running via Spring preloader in process *****
      create  app/controllers/users/confirmations_controller.rb
      create  app/controllers/users/passwords_controller.rb
      create  app/controllers/users/registrations_controller.rb
      create  app/controllers/users/sessions_controller.rb
      create  app/controllers/users/unlocks_controller.rb
      create  app/controllers/users/omniauth_callbacks_controller.rb
===============================================================================

②追加でroutes.rbに以下のような記述が必要なことをお知らせしてくれている
Some setup you must do manually if you haven't yet:

  Ensure you have overridden routes for generated controllers in your routes.rb.
  For example:

       ⬇︎  追記内容 ⬇︎
    Rails.application.routes.draw do
      devise_for :users, controllers: {
        sessions: 'users/sessions'
      }
    end

===============================================================================
補足) このコマンドを実行しない限り中身をみることはできませんが、ユーザー登録時やサインイン時などは上記のコントローラで処理をしています。

上記の指示に習って、routes.rbを編集します。

config/routes.rb
# before
Rails.application.routes.draw do
  devise_for :users

# after
Rails.application.routes.draw do
  devise_for :users, controllers: {
    registrations: "users/registrations",
  }

# 今回は”編集”に関することなので、registrations_controllerのみ記述しています。

2. registrations_controller.rbの編集

パスワードなしで編集できるように登録(編集)時に処理を行うコントローラの記述を変更します。ファイル下部のprotected のコメントアウトを外したうえで、その下にupdate_resourceメソッドを追記してください。

registrations_controller.rb
  # コメントアウト外す
  protected

  #必須  更新(編集の反映)時にパスワード入力を省く
  def update_resource(resource, params)
    resource.update_without_password(params)
  end

  #任意  更新後のパスを指定
  def after_update_path_for(resource)
    user_path(@user.id)
  end

これで更新時にパスワードを入力しなくてもよくなりました。
ただ編集画面にパスワードの入力欄が残ったままなので、もう少し変更を加えます。

3. edit.html.erbの編集

最後に編集画面からパスワード入力欄をなくします。
まずはdeviseのビューを編集できるようにするため、以下を行ってください。

ターミナル
$ rails g devise:views

Running via Spring preloader in process *****
      ==== 必要な部分以外省略 ====
      create    app/views/devise/registrations/edit.html.erb

続いてビューからパスワード欄の記述を削除します。

devise/registrations/edit.html.erb
  # 以下の記述を削除
  <div class="field">
    <%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
    <%= f.password_field :password, autocomplete: "new-password" %>
    <% if @minimum_password_length %>
      <br />
      <em><%= @minimum_password_length %> characters minimum</em>
    <% end %>
  </div>

  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "new-password" %>
  </div>

  <div class="field">
    <%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
    <%= f.password_field :current_password, autocomplete: "current-password" %>
  </div>

以上で変更作業は終了です。
最後に動作確認を行いましょう。

動作確認

今回は変更後の様子を分かりやすくするため、更新後に遷移する詳細ページのビューを以下のようにしています。ユーザー情報にnicknameも追加しています。

事前に登録した内容

nickname: sample
email: sample@sample
pasword: ******

ユーザー詳細ページ

Image from Gyazo

登録編集画面は以下のようになっています。

ユーザー登録編集ページ

Image from Gyazo


パスワードの入力欄がすべてなくなっていますね。
それではユーザー情報を更新してみましょう。
nicknameをテストユーザーに変更し、updateボタンを押します。
すると、、、

変更内容

nickname: sample => テストユーザー

ユーザー詳細ページ(更新後)

Image from Gyazo


名前が sample から テストユーザー に変わりましたね。
以上で終了となります。お疲れ様でした。
また最後までお読みいただきありがとうございました。

補足

補足1)3. edit.html.erbの編集で任意事項(更新後にユーザー詳細ページへ遷移する)を記述した方は、変数@userを使用するため、usersコントローラのshowアクションで以下のように定義しておく必要があります。

controllers/users_controller.rb
  def show
      @user = User.find(params[:id])
  end

補足2)ユーザー詳細ページのコードは以下の通りです。同じshowアクションを使い、リンクによってマイページと他ユーザーのページが切り替わるようにしてあります。

users/show.html.erb
#マイページの記述
<% if params[:id].nil? || @user.id == current_user.id %>
  <h1>ようこそ<%= current_user.nickname %>さん</h1>
  ここはusers#showページです。
  <p>名前:<%= current_user.nickname %></p>
  <p>登録email<%= current_user.email %></p>
  <br>
  アカウントを編集しますか?
  <%= link_to "はい", edit_user_registration_path %>
  <br>
  ログアウトしますか?
  <%= link_to "はい", destroy_user_session_path, method: :delete %>
#他ユーザーページ記述
<% else %>
  <h2>ここは<%= @user.nickname %>さんのページです</h2>
  <p>名前:<%= @user.nickname %></p>
  <p>登録email:<%= @user.email %></p>
<% end %>

20
17
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
20
17