個人アプリ開発 マイページ作成

ユーザー編集機能作成、プロフィール画像の追加

一度ざっと作成しましたが、編集後updateがうまくできずハマってしまいました。

→スペルミスや記述漏れなど穴が開くほど確認笑
→binding.pryで確認するが、ちゃんと送れている
→理由が不明だった為、devise箇所を作り直しました。

元々user_contorollerのみ使用するお手軽な(?)作成方法をしていた為、

$ rails g devise:controllers users

で作成したregistrations_controller.rbとapprication_controller.rbを編集していきました。
が、結果上手くいかず。

ここでふとviewに

= devise_error_messages!

を入れて、エラー内容を確認できることに気がつきました。

▶︎”password is invailed”のエラー文が出た為、まさかと思いつつバリデーションを確認したところ、passwordに変なバリデーションがかかっていました。なので保存がされなかった。
修正して(一旦バリデーションを外して)、解決。

・まず原因を特定すること
・解決案を柔軟に出すこと
非常に勉強になりました。

作業一覧

コントローラ作成
$ rails g devise:controllers users
モデル編集

passwordのバリデーションを修正
mount_uploader :image, ImageUploader #追加
(テーブルにはimageカラム作成済み)

コントローラ編集
[application_controller]
class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  # ログイン済ユーザーのみにアクセスを許可する
  before_action :authenticate_user!

  # deviseコントローラーにストロングパラメータを追加する          
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected
  def configure_permitted_parameters
    # サインアップ時にnameのストロングパラメータを追加
    devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
    # アカウント編集の時にnameとprofileのストロングパラメータを追加
    devise_parameter_sanitizer.permit(:account_update, keys: [:name, :image])
  end

end
編集時のview
[devise_registrations_edit.html.haml]
#account-page.account-page
  .account-page__inner.clearfix
    .account-page__inner--left.account-page__header
      %h2 Edit Account
      %h5 アカウントの編集
      = link_to "ログアウト", '#', class: 'btn'
      = link_to "トップページに戻る", :back, class: 'btn'
    .account-page__inner--right.user-form
      = form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f|
        = devise_error_messages!
        .field
          .field-label
            = f.label :ニックネーム
          .field-input
            = f.text_field :name, autofocus: true
        .field
          .field-label
            = f.label :メールアドレス
          .field-input
            = f.email_field :email
        .field
          .field-label
            = f.label :アカウント画像
          .field-input
            = f.file_field :image
        .actions
          = f.submit "Update", class: 'btn'
ルーティング
resources :users, only: [:show]

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

usersのshowへのルーティングを作成。
その下の「devise_for」の記述でregistrations_controller.rbの内容を有効にする

コントローラ
[app/controllers/users/registrations_controller.rb]
protected
  # アカウント編集後、プロフィール画面に移動する
  def after_update_path_for(resource)
    user_path(id: current_user.id)
  end
[app/controllers/users/apprication_controller.rb]
  # ログイン後、トップページに移動する
  def after_sign_in_path_for(resource)
    root_path
  end
プロフィール画面作成
$ rails g controller Users show

deviseのusersとは別にusers_controller.rbとusers/show.html.erbを生成。

[app/controllers/users_controller.rb]
class UsersController < ApplicationController
  def show
    @user = User.find(params[:id]) #追記
  end
end
%h1 Users#show
%p Find me in app/views/users/show.html.haml
= link_to "編集", edit_user_registration_path

一旦仮で作成

すごく無駄な遠回りをしてしまいました。
ただdeviseについて理解を深めることができた点は良かったと思います。
明日以降、マイページの中身を作成していきます。