【Rails4】Deviseでユーザーを楽に管理する(更新・表示編)

前回に引き続きdeviceを使い今回はユーザー情報の更新処理を実装する。

まずはeditにfirst_name,last_nameを追加。
app/views/devise/registrations/edit.html.slim

h2
  | Edit
  = resource_name.to_s.humanize
= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f|
  = devise_error_messages!
  .field
    = f.label :first_name, "first_name"
    br
    = f.text_field :first_name, autofoucus: true
  .field
    = f.label :last_name, "last_name"
    br
    = f.text_field :last_name
  .field
    = f.label :email
    br
    = f.email_field :email
  - if devise_mapping.confirmable? && resource.pending_reconfirmation?
    div
      | Currently waiting confirmation for:
      = resource.unconfirmed_email
  .field
    = f.label :password
    i
      | (leave blank if you don't want to change it)
    br
    = f.password_field :password, autocomplete: "off"
  .field
    = f.label :password_confirmation
    br
    = f.password_field :password_confirmation, autocomplete: "off"
  .field
    = f.label :current_password
    i
      | (we need your current password to confirm your changes)
    br
    = f.password_field :current_password, autocomplete: "off"
  .actions
    = f.submit "Update"
h3
  | Cancel my account
p
  | Unhappy?
  = button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete
= link_to "Back", :back

このままだとStrong ParametersにひっかかるのでControllerを編集

app/controllers/users/registrations_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController
 before_action :configure_sign_up_params, only: [:create]
 before_action :configure_account_update_params, only: [:update]

  # GET /resource/sign_up
  # def new
  #   super
  # end

  # POST /resource
  #def create
  #   super
  # end

  # GET /resource/edit
  # def edit
  #   super
  # end

  # PUT /resource
  # def update
  #   super
  # end

  # DELETE /resource
  # def destroy
  #   super
  # end

  # GET /resource/cancel
  # Forces the session data which is usually expired after sign
  # in to be expired now. This is useful if the user wants to
  # cancel oauth signing in/up in the middle of the process,
  # removing all OAuth session data.
  # def cancel
  #   super
  # end

   protected

  # If you have extra params to permit, append them to the sanitizer.
   def configure_sign_up_params
     devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name])
   end

  # If you have extra params to permit, append them to the sanitizer.
   def configure_account_update_params
    devise_parameter_sanitizer.permit(:account_update, keys: [:first_name, :last_name])
   end

  # The path used after sign up.
  # def after_sign_up_path_for(resource)
  #   super(resource)
  # end

  # The path used after sign up for inactive accounts.
  # def after_inactive_sign_up_path_for(resource)
  #   super(resource)
  # end
end


これで更新処理の実装は完了。
ただ、このままでは更新したユーザー情報が表示できないので表示処理を実装する。

deviceにはshowとindexは無いため自分で用意する。

$ rails g controller Users index show


Controllerを修正(deviceの方ではないので注意)

class UsersController < ApplicationController
  def index
    @users = User.all
  end

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


indexとshowを修正
app/views/users/index.html.slim

ul
  -@users.each do |user|
    li
      =link_to user.first_name<<' '<< user.last_name, user

※ink_to user.first_name<<' '<< user.last_name に関してはModelにfullnameでも追加した方がいい


app/views/users/show.html.slim

p =@user.first_name
p =@user.last_name


最後にルーティングにindexとshowを追加
config/routes.rb

Rails.application.routes.draw do
  get 'users/index'

  get 'users/show'

  devise_for :users, :controllers => {
    :registrations => 'users/registrations'
  }

  resources :users, :only => [:index, :show]

これでユーザー 一覧と詳細が実装された。