2012-02-17 3 views
15

재정의 된 암호 암호 컨트롤러의 오류 메시지는 어떻게 사용자 정의 할 수 있습니까?암호 변경시 오류 메시지 무시 방법

class PasswordsController < Devise::PasswordsController 
    def create 
    self.resource = resource_class.send_reset_password_instructions(params[resource_name]) 

    if resource.errors.empty? 
     set_flash_message(:notice, :send_instructions) if is_navigational_format? 
     respond_with resource, :location => home_path 
    else 
     binding.pry 
     flash[:devise_password_error] = (resource.errors.map do |key, value| 
     value.capitalize 
     end).flatten.join('|') 
     redirect_to home_path and return 
    end 
    end 
    def edit 
    self.resource = resource_class.new 
    resource.reset_password_token = params[:reset_password_token] 
    end 
end 

resource.errors이 방법에 사용할 수 있지만 그러한 Email not foundEmail can't be blank 같은 기본 메시지가 들어 있습니다. 이 메시지를 사용자 지정해야합니다. 내 사용자 모델에서 :validatable을 제거하고 사용자 정의 유효성 검사기를 추가하려고 시도했지만이 작업은 Devise :: RegistrationsController에서 파생 된 사용자 정의 등록 컨트롤러에서만 작동하며 사용자 정의 암호 컨트롤러에서는 작동하지 않습니다.

해결책이 있습니까?

+0

당신이이 문제를 해결 했습니까? –

+0

나는 이것이 오래되었다는 것을 알고 있지만 좀 더 일반적이고 깨끗한 접근법을 원한다면 [this answer] (http://stackoverflow.com/a/18578028/1964165)를 확인한다. – akhanubis

답변

7

고안 메시지는 당신이 무시하려는 어떤 메시지 모르겠어요 설정/로케일/devise.en.yml

에 위치하고 있습니다,하지만 당신은 그렇게 할 곳이다.

+3

재정의하려는 메시지가 거기에 없습니다. 변경하고 싶은 메시지는 유효성 검증이 실패했을 때 사용되는 기본 메시지이고'config/locales/devise.en.yml'에는 장치 정보 메시지가 들어 있습니다. – RomanKapitonov

+0

해당 메시지는 실제로 거기에 없지만 추가 할 수 있으며 작동합니다. 위의 내 대답을 참조하십시오. – Justin

0

그것은 이상적인,하지만 난이 다음과 같은 작업있어 한 this related ticket에 따라 아니다 (I 해킹의 조금 알고있는,하지만 작동) :

module DeviseHelper 
    def devise_error_messages! 
    resource.errors.full_messages.map { |msg| msg == 'Email not found' ? 'The email address you entered could not be found. Please try again with other information.' : msg }.join('<br/>') 
    end 
end 

라는 모듈이 넣어 그들은 기본적으로 존재하지 않습니다, 당신의 /app/helpers 디렉토리에 devise_helper.rb

15

대답은 설정/로케일/devise.en.yml을 수정하는 것입니다하지만 당신은 설정을 추가해야합니다. 이것에 대한

en: 
    activerecord: 
    errors: 
     models: 
     user: 
      attributes: 
      password: 
       confirmation: "does not match" 
       too_short: "is too short (minimum is %{count} characters)" 

신용 나를 위해 거의 같은 question 대답 Vimsha로 이동합니다.

+1

최소값을 하드 코딩하지 말고 오류 메시지에 % {count}를 사용해야합니다. 카운트가 범위 유효성 검사기에 의해 I18n.t에 전달되므로 항상 장치 설정과 일치합니다. – ReggieB

0

당신의 routes.rb이 추가

devise_for :users, controllers: { passwords: 'passwords' } 

또는

devise_for :users, :controllers => { :passwords => 'passwords' }