##開発環境
rails 5.0.0.1
devise 4.2.0
##エラーコード
現状、編集を保存しようとするとこちらのエラーメッセージが表示されます。
エラーにより、この user を保存できません: Current password translation missing: ja.activerecord.errors.models.user.attributes.current_password.invalid
##前提
プロフィール編集にあたって、デフォルトDeviseでは現在のパスワード入力が必須です。パスワード入力なしでプロフィール情報を更新できるようにしたいです。
現在は、Facebookログインのみ実装しています。
deviseで現在のパスワード無しでuserを更新する
上記のサイトを参考にやってみたのですが、2012年と情報が古いのか動きません。。
##該当するソースコード
users_controller.rb
Ruby
1class UsersController < ApplicationController 2 def show 3 @user = User.find(params[:id]) 4 end 5 6 private 7 8 def user_params 9 params.require(:user).permit(:name, :email, :image, :background) 10 end 11end
user.rb
Ruby
1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable and :omniauthable 4 devise :database_authenticatable, :registerable, 5 :recoverable, :rememberable, :trackable, :validatable, 6 :omniauthable 7 8 class << self 9 def find_for_facebook_oauth(auth) 10 where(provider: auth.provider, uid: auth.uid).first_or_create do |user| 11 user.email = auth.info.email 12 user.password = Devise.friendly_token[0,20] 13 user.name = auth.info.name 14 end 15 end 16 17 def create_unique_string 18 SecureRandom.uuid 19 end 20 21 def create_unique_email 22 User.create_unique_string + "@example.com" 23 end 24 end 25 26 def update_without_current_password(params, *options) 27 params.delete(:current_password) 28 29 if params[:password].blank? 30 params.delete(:password) 31 params.delete(:password_confirmation) if params[:password_confirmation].blank? 32 end 33 34 clean_up_passwords 35 update_attributes(params, *options) 36 end 37end
registrations_controller.rb
Ruby
1class RegistrationsController < Devise::RegistrationsController 2 def update 3 @user = User.find(current_user.id) 4 if @user.update_without_current_password(params[:user]) 5 sign_in @user, bypass: true 6 set_flash_message :notice, :updated 7 redirect_to after_update_path_for(@user) 8 else 9 render 'edit' 10 end 11 end 12end
registrations/edit.html
Ruby
1<% provide(:title, "アカウントの編集") %> 2<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %> 3<%= devise_error_messages! %> 4<section class="mypage-profile"> 5 <div class="container mypage-form title"> 6 <div class="title-text"> 7 <h2>プロフィールの編集</h2> 8 </div> 9 <form> 10 <div class="form-group"> 11 <%= f.label :name, "ユーザー名" %> 12 <%= f.text_field :name, :placeholder => "ユーザー名", :value => @user.name, class: "form-control" %> 13 </div> 14 <div class="form-group"> 15 <%= f.label :current_password, "現在のパスワード" %> 16 <%= f.password_field :current_password, :placeholder => "現在のパスワード", class: "form-control", autocomplete: "off" %> 17 </div> 18 <%= f.submit "登録内容を変更する", class: "btn btn-form" %><br /> 19 </form> 20 <% end %> 21 </div> 22</section>
現在のパスワードを消したいです!
すみませんが、どうぞよろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/06/19 04:24
2017/06/19 05:14
2017/06/19 07:46
2017/06/19 12:43