简体   繁体   中英

Validations running on devise password change

how can I stop validations running when entrant (user) updates password. Users cannot update their password.

    class Entrant < ActiveRecord::Base

    devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable

   validates :first_name, presence: true, on: :create
   validates :last_name, presence: true, on: :create

   validates :title, presence: true, on: :update

Attempted the steps described here -

How To: Allow users to edit their password

But not sure if this is necessary as I'm trying to do only standard behaviour.

Routes -

Rails.application.routes.draw do



  get 'messages/judge_logout/' => "messages#judge_logout", :as => 'messages_judge_logout'
  get 'exporter/entrant'
  get 'exporter/entries'
  get 'exporter/results'
  get 'exporter/registered_not_entered'
  #get 'judge/index'
  #get 'judge/show'
  resources :judge, :only =>[:show,:index]

  get 'all_entries/list', as: 'all_entries_list'
  get 'all_entries/final_scores', as: 'all_scores_list'
  get '/all_entries/list', as: :admin_root
  resources :entries

  devise_for :entrants, :controllers => { registrations: 'registrations' }
  devise_for :judges, :controllers =>  { sessions: 'judges/sessions' }
  devise_for :admins

  resources :charges
  resources :votes, :only =>[:create,:update]

Controller -

class RegistrationsController < Devise::RegistrationsController


  private

  def sign_up_params
    params.require(:entrant).permit(:first_name, :last_name, :email, :password, :password_confirmation)
  end

  def account_update_params
    params.require(:entrant).permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password, :title, :address, :postcode, :main_phone_number, :secondary_phone_number, :website, :dob, :place_of_birth, :place_of_education, :degree_attained, :how_did_you_hear_about_newlight, :terms_of_service)
  end


  def after_update_path_for(resource)
    #edit_entrant_registration_path(resource)
    entries_path
  end

  def after_sign_up_path_for(resource)
    edit_entrant_registration_path(resource)
  end

  def after_sign_in_path_for(resource)
    entries_path
  end

end

I found that the problem is only because the validations are running on update, which means when the model password is changed the validations are running. Adding an unless conditional stopped the validation running.

Answer to problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM