2012-11-09 84 views
1

我有這樣的驗證結構:紅寶石軌道上。如何格式化errors.full_messages?

validates :upload_file_name, :presence => true, 
           :format  =>{:with => %r{\.(jpg)$}i,:message =>"should have an extension .jpg"} 

validates_uniqueness_of :upload_file_name, :message => "is already in the database" 

當我輸入軌控制檯,我得到了以下的答案

> s.errors.full_messages 

=> ["Upload file name is already in the database"] 

我需要的信息是唯一的「是已經在數據庫中」不「上傳文件名」 我該怎麼做?這second solution of this problem並沒有幫助我,因爲我得到一個錯誤:

en: 
    activerecord: 
    errors: 
     full_messages: 
      format:"%{attribute} %{message}" 


1.9.3-p286 :002 > s.save() 
    (0.2ms) begin transaction 
    Upload Exists (0.3ms) SELECT 1 AS one FROM "uploads" WHERE "uploads"."upload_file_name" = 'Katja.jpg' LIMIT 1 
    (0.3ms) rollback transaction 
Psych::SyntaxError: (/home/katja/Desktop/tors-jquery-fileupload-rails-paperclip-example-b0b9934/config/locales/en.yml): found character that cannot start any token while scanning for the next token at line 6 column 1 
    from /home/katja/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/psych.rb:203:in `parse' 
    from /home/katja/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/psych.rb:203:in `parse_stream' 
    from /home/katja/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/psych.rb:151:in `parse' 
    from /home/katja/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/psych.rb:127:in `load' 
    from /home/katja/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/psych.rb:297:in `block in load_file' 
    from /home/katja/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/psych.rb:297:in `open' 
    from /home/katja/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/psych.rb:297:in `load_file' 
    from /home/katja/.rvm/gems/ruby-1.9.3-p286/gems/i18n-0.6.0/lib/i18n/backend/base.rb:171:in `load_yml' 
    from /home/katja/.rvm/gems/ruby-1.9.3-p286/gems/i18n-0.6.0/lib/i18n/backend/base.rb:157:in `load_file' 
    from /home/katja/.rvm/gems/ruby-1.9.3-p286/gems/i18n-0.6.0/lib/i18n/backend/base.rb:15:in `block in load_translations' 
    from /home/katja/.rvm/gems/ruby-1.9.3-p286/gems/i18n-0.6.0/lib/i18n/backend/base.rb:15:in `each' 
    from /home/katja/.rvm/gems/ruby-1.9.3-p286/gems/i18n-0.6.0/lib/i18n/backend/base.rb:15:in `load_translations' 
    from /home/katja/.rvm/gems/ruby-1.9.3-p286/gems/i18n-0.6.0/lib/i18n/backend/simple.rb:57:in `init_translations' 
    from /home/katja/.rvm/gems/ruby-1.9.3-p286/gems/i18n-0.6.0/lib/i18n/backend/simple.rb:71:in `lookup' 
    from /home/katja/.rvm/gems/ruby-1.9.3-p286/gems/i18n-0.6.0/lib/i18n/backend/base.rb:26:in `translate' 
    from /home/katja/.rvm/gems/ruby-1.9.3-p286/gems/i18n-0.6.0/lib/i18n.rb:156:in `block in translate' 
... 25 levels... 
    from /home/katja/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.6/lib/active_record/validations.rb:50:in `save' 
    from /home/katja/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.6/lib/active_record/attribute_methods/dirty.rb:22:in `save' 
    from /home/katja/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.6/lib/active_record/transactions.rb:241:in `block (2 levels) in save' 
    from /home/katja/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.6/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status' 
    from /home/katja/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction' 
    from /home/katja/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.6/lib/active_record/transactions.rb:208:in `transaction' 
    from /home/katja/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.6/lib/active_record/transactions.rb:293:in `with_transaction_returning_status' 
    from /home/katja/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.6/lib/active_record/transactions.rb:241:in `block in save' 
    from /home/katja/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.6/lib/active_record/transactions.rb:252:in `rollback_active_record_state!' 
    from /home/katja/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.6/lib/active_record/transactions.rb:240:in `save' 
    from (irb):2 
    from /home/katja/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.6/lib/rails/commands/console.rb:47:in `start' 
    from /home/katja/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.6/lib/rails/commands/console.rb:8:in `start' 
    from /home/katja/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.6/lib/rails/commands.rb:41:in `<top (required)>' 
    from script/rails:6:in `require' 

在先進的感謝。

回答

1

解決方案:

s.errors.message[:upload_file_name] 
+0

我得到:'NoMethodError異常:未定義的方法\'消息'爲#'' – panzi

1

FWIW你得到的錯誤是因爲你使用製表符縮進,而不是空間,這在YAML不允許的。