在设计密码更改时运行的验证

时间:2017-06-07 15:10:14

标签: ruby-on-rails devise

当进入者(用户)更新密码时,如何停止运行验证。用户无法更新密码。

    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

尝试了此处描述的步骤 -

How To: Allow users to edit their password

但不确定这是否有必要,因为我只想做标准行为。

路线 -

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]

控制器 -

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

1 个答案:

答案 0 :(得分:0)

我发现问题只是因为验证在更新时运行,这意味着更改模型密码时验证正在运行。添加unless条件会使验证停止运行。

Answer to problem