前提・実現したいこと
devise でマイページ編集機能を実装したので、毎回更新できるようにしたい。
該当のソースコード
ruby
1class ApplicationController < ActionController::Base 2 protect_from_forgery with: :exception 3 include SessionsHelper 4 5 # ログイン後に遷移するページ 6 def after_sign_in_path_for(resource) 7 mypage_companies_path(resource) 8 end 9 10 def after_sign_in_path_for(resource) 11 case resource 12 13 when Company 14 mypage_companies_path(resource) 15 16 when UserDevise 17 mypage_user_devises_path(resource) 18 end 19 end 20 21 22 23 24 # deviseコントローラーにストロングパラメータを追加する 25 before_action :configure_permitted_parameters, if: :devise_controller? 26 27 protected 28 def configure_permitted_parameters 29 # サインアップ時にnameのストロングパラメータを追加 30 # devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :furigane]) 31 32 33 # アカウント編集の時にnameとprofileのストロングパラメータを追加 34 devise_parameter_sanitizer.permit(:account_update, keys: [:name, :furigane, :birthday, :sex, :profession, :career, :number, :address, :pr]) 35 end 36end 37
app/controllers/user_devises/registrations_controller.rb
ruby
1# frozen_string_literal: true 2 3class UserDevises::RegistrationsController < Devise::RegistrationsController 4 # before_action :configure_sign_up_params, only: [:create] 5 # before_action :configure_account_update_params, only: [:update] 6 7 # GET /resource/sign_up 8 # def new 9 # super 10 # end 11 12 # POST /resource 13 # def create 14 # super 15 # end 16 17 # GET /resource/edit 18 # def edit 19 # super 20 # end 21 22 # PUT /resource 23 # def update 24 # super 25 # end 26 27 # DELETE /resource 28 # def destroy 29 # super 30 # end 31 32 # GET /resource/cancel 33 # Forces the session data which is usually expired after sign 34 # in to be expired now. This is useful if the user wants to 35 # cancel oauth signing in/up in the middle of the process, 36 # removing all OAuth session data. 37 # def cancel 38 # super 39 # end 40 41 # protected 42 43 # If you have extra params to permit, append them to the sanitizer. 44 # def configure_sign_up_params 45 # devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute]) 46 # end 47 48 # If you have extra params to permit, append them to the sanitizer. 49 # def configure_account_update_params 50 # devise_parameter_sanitizer.permit(:account_update, keys: [:attribute]) 51 # end 52 53 #アカウント登録後のリダイレクト先 54 def after_sign_up_path_for(resource) 55 56 end 57 58 #アカウント情報、プロフィール編集後のリダイレクト先 59 def after_update_path_for(resource) 60 # マイページへ遷移 61 user_devise_path(resource.id) 62 end 63 64 # The path used after sign up for inactive accounts. 65 # def after_inactive_sign_up_path_for(resource) 66 # super(resource) 67 # end 68end
app/views/devise/registrations/edit.html.erb
ruby
1<div class="p-edit"> 2 <% if t('.title', resource: resource.model_name.human) == "User devise編集" %> 3 <% @name = params[:name] %> 4 5 <% if @name == "アカウント情報変更" %> 6 <h2 class="c-heading__h2--bold u-mb20">アカウント情報変更</h2> 7 8 <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %> 9 <%= render "devise/shared/error_messages", resource: resource %> 10 11 <div class="p-edit__field"> 12 <%= f.label :メールアドレス %> 13 <%= f.email_field :email, autofocus: true, autocomplete: "email" %> 14 </div> 15 16 <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %> 17 <div><%= t('.currently_waiting_confirmation_for_email', email: resource.unconfirmed_email) %></div> 18 <% end %> 19 20 <div class="p-edit__field"> 21 <%= f.label :現在のパスワード %> 22 <%= f.password_field :current_password, autocomplete: "current-password" %> 23 </div> 24 25 <div class="p-edit__field"> 26 <%= f.label :新しいパスワード %> 27 <% if @minimum_password_length %> 28 <p><%= t('devise.shared.minimum_password_length', count: @minimum_password_length) %></p> 29 <% end %> 30 <%= f.password_field :password, autocomplete: "new-password" %> 31 </div> 32 33 <div class="p-edit__field"> 34 <%= f.label :新しいパスワード【確認】 %> 35 <%= f.password_field :password_confirmation, autocomplete: "new-password" %> 36 </div> 37 38 39 <div class="p-edit__actions"> 40 <%= f.submit t('.update'), class: "c-btn__change" %> 41 </div> 42 <% end %> 43 44 45 46 47 <% elsif @name == "プロフィール変更" %> 48 49 <h2 class="c-heading__h2--bold u-mb20">プロフィール変更</h2> 50 51 <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %> 52 <%= render "devise/shared/error_messages", resource: resource %> 53 54 55 <%# ここまでの情報がないと、編集内容がなぜか反映されない %> 56 <div class="p-edit__field" style="display: none;"> 57 <%= f.label :メールアドレス %> 58 <%= f.email_field :email, autofocus: true, autocomplete: "email" %> 59 </div> 60 61 <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %> 62 <div style="display: none;"><%= t('.currently_waiting_confirmation_for_email', email: resource.unconfirmed_email) %></div> 63 <% end %> 64 65 <div class="p-edit__field" style="display: none;"> 66 <%= f.label :現在のパスワード %> 67 <%= f.password_field :current_password, autocomplete: "current-password" %> 68 </div> 69 70 <div class="p-edit__field" style="display: none;"> 71 <%= f.label :新しいパスワード %> 72 <% if @minimum_password_length %> 73 <p><%= t('devise.shared.minimum_password_length', count: @minimum_password_length) %></p> 74 <% end %> 75 <%= f.password_field :password, autocomplete: "new-password" %> 76 </div> 77 78 <div class="p-edit__field" style="display: none;"> 79 <%= f.label :新しいパスワード【確認】 %> 80 <%= f.password_field :password_confirmation, autocomplete: "new-password" %> 81 </div> 82 <%# ここまでの情報がないと、編集内容がなぜか反映されない %> 83 84 <div class="p-edit__field"> 85 <%= f.label :氏名 %> 86 <%= f.text_field :name, autofocus: true, autocomplete: "name" %> 87 </div> 88 89 <div class="p-edit__field"> 90 <%= f.label :ふりがな %> 91 <%= f.text_field :furigane, autofocus: true, autocomplete: "furigane" %> 92 </div> 93 94 <div class="p-edit__field"> 95 <%= f.label :生年月日 %> 96 <%= f.text_field :birthday, autofocus: true, autocomplete: "birthday" %> 97 </div> 98 99 <div class="p-edit__field"> 100 <%= f.label :性別 %> 101 <%= f.text_field :sex, autofocus: true, autocomplete: "sex" %> 102 </div> 103 104 <div class="p-edit__field"> 105 <%= f.label :現在の職業 %> 106 <%= f.text_field :profession, autofocus: true, autocomplete: "profession" %> 107 </div> 108 109 <div class="p-edit__field"> 110 <%= f.label :直近の職務経歴 %> 111 <%= f.text_area :career, autofocus: true, autocomplete: "career" %> 112 </div> 113 114 <div class="p-edit__field"> 115 <%= f.label :電話番号 %> 116 <%= f.text_field :number, autofocus: true, autocomplete: "number" %> 117 </div> 118 119 <div class="p-edit__field"> 120 <%= f.label :住所 %> 121 <%= f.text_field :address, autofocus: true, autocomplete: "address" %> 122 </div> 123 124 <div class="p-edit__field"> 125 <%= f.label :備考・PR %> 126 <%= f.text_area :pr, autofocus: true, autocomplete: "pr" %> 127 </div> 128 129 <div class="p-edit__actions"> 130 <%= f.submit t('.update'), class: "c-btn__change" %> 131 </div> 132 <% end %> 133 <% end %> 134 135 136 137 138 <h3><%= t('.cancel_my_account') %></h3> 139 140 <p><%= t('.unhappy') %> <%= button_to t('.cancel_my_account'), registration_path(resource_name), data: { confirm: t('.are_you_sure') }, method: :delete %></p> 141 142 <%= link_to t('devise.shared.links.back'), :back %> 143 144 145 <% else %> 146 147 <h2>会社情報変更</h2> 148 149 <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %> 150 <%= render "devise/shared/error_messages", resource: resource %> 151 152 <div class="field"> 153 <%= f.label :email %><br /> 154 <%= f.email_field :email, autofocus: true, autocomplete: "email" %> 155 </div> 156 157 158 <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %> 159 <div><%= t('.currently_waiting_confirmation_for_email', email: resource.unconfirmed_email) %></div> 160 <% end %> 161 162 <div class="field"> 163 <%= f.label :password %> <i>(<%= t('.leave_blank_if_you_don_t_want_to_change_it') %>)</i><br /> 164 <%= f.password_field :password, autocomplete: "new-password" %> 165 <% if @minimum_password_length %> 166 <br /> 167 <em><%= t('devise.shared.minimum_password_length', count: @minimum_password_length) %></em> 168 <% end %> 169 </div> 170 171 <div class="field"> 172 <%= f.label :password_confirmation %><br /> 173 <%= f.password_field :password_confirmation, autocomplete: "new-password" %> 174 </div> 175 176 <div class="field"> 177 <%= f.label :current_password %> <i>(<%= t('.we_need_your_current_password_to_confirm_your_changes') %>)</i><br /> 178 <%= f.password_field :current_password, autocomplete: "current-password" %> 179 </div> 180 181 <div class="actions"> 182 <%= f.submit t('.update') %> 183 </div> 184 <% end %> 185 186 <h3><%= t('.cancel_my_account') %></h3> 187 188 <p><%= t('.unhappy') %> <%= button_to t('.cancel_my_account'), registration_path(resource_name), data: { confirm: t('.are_you_sure') }, method: :delete %></p> 189 190 <%= link_to t('devise.shared.links.back'), :back %> 191 192 <% end %> 193</div>
補足情報(FW/ツールのバージョンなど)
Rails 5.1.6
よろしくお願いいたします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/09/28 09:01