2012-02-17 14 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 found और Email can't be blank के रूप में डिफ़ॉल्ट संदेश हैं। मुझे इन संदेशों को कस्टमाइज़ करने की ज़रूरत है। मैंने अपने उपयोगकर्ता मॉडल से :validatable को हटाने और कस्टम सत्यापनकर्ताओं को जोड़ने का प्रयास किया है, लेकिन यह केवल मेरे कस्टम पंजीकरण नियंत्रक के लिए काम करता है जो डेविस :: रजिस्ट्रेशन नियंत्रक से व्युत्पन्न है और कस्टम पासवर्ड नियंत्रक के लिए नहीं।

क्या कोई समाधान है?

+0

आप इस समाधान हो गया? –

+0

मुझे पता है कि यह पुराना है, लेकिन अधिक सामान्य और साफ दृष्टिकोण के लिए [यह जवाब] (http://stackoverflow.com/a/18578028/1964165) देखें। – akhanubis

उत्तर

7

वसीयत संदेशों config/स्थानों/devise.en.yml

में स्थित हैं मुझे यकीन है कि जो संदेश आप ओवरराइड करने के लिए कोशिश कर रहे हैं नहीं कर रहा हूँ, लेकिन यह है कि जहां आप ऐसा चाहते हैं।

+3

जिन संदेशों को मैं ओवरराइड करना चाहता हूं वे वहां स्थित नहीं हैं। ये संदेश मैं बदलना चाहता हूं, जब सत्यापन विफल रहता है और 'config/locales/devise.en.yml' में डिवाइसेज सूचना संदेश होते हैं तो डिफ़ॉल्ट संदेश उपयोग किए जाते हैं। – RomanKapitonov

+0

वे संदेश वास्तव में वहां नहीं हैं, लेकिन आप उन्हें जोड़ सकते हैं और यह काम करेगा। ऊपर मेरा जवाब देखें। – Justin

0

यह आदर्श नहीं है, लेकिन this related ticket मैं इसे निम्नलिखित के साथ काम कर गया है, उसके आधार है (जो मैं जानता हूँ कि एक हैक का एक सा है, लेकिन यह काम करता है):

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

जवाब वे डिफ़ॉल्ट रूप से वहाँ नहीं कर रहे हैं config/स्थानों/devise.en.yml संशोधित करने के लिए है, लेकिन आप सेटिंग्स को जोड़ना होगा। इस के लिए

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

क्रेडिट Vimsha जो लगभग मेरे लिए एक ही question जवाब को जाता है।

+1

आपको न्यूनतम मूल्य को हार्ड कोडिंग के बजाय त्रुटि संदेश में% {count} का उपयोग करना चाहिए। रेंज वैलिडेटर द्वारा I18n.t पर गिनती की जाती है, और इसलिए हमेशा तैयार सेटिंग से मेल खाती है। – ReggieB

0

अपने routes.rb को यह जोड़े

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

या

devise_for :users, :controllers => { :passwords => 'passwords' } 
संबंधित मुद्दे