【问题标题】:Carrierwave Translation Missing on Validation File Size验证文件大小缺少载波转换
【发布时间】:2013-04-28 15:52:39
【问题描述】:

我试试这个https://gist.github.com/chrisbloom7/1009861 来验证carrierwave 上的文件大小

  1. 我创建了lib/file_size_validator.rb
  2. 我已经对文件 yml 进行了一些翻译(wrong_size、size_too_small、size_too_big、number.human.storage_units.units.mb)
  3. require 'file_size_validator' 放到我的模型中
  4. 重启服务器等...

没有什么可以忽略的

但是当我尝试上传大小为 24MB 的视频时,我收到了错误:

I18n::MissingTranslationData in VideosController#create

translation missing: id.number.human.storage_units.units.mb

lib/file_size_validator.rb:51:in `block in validate_each'
lib/file_size_validator.rb:42:in `each'
lib/file_size_validator.rb:42:in `validate_each'
app/controllers/videos_controller.rb:67:in `create'

这里的id.yml 看起来像:

id:
  errors:
    messages:
      wrong_size: "is the wrong size (should be %{file_size})"
      size_too_small: "is too small (should be at least %{file_size})"
      size_too_big: "is too big (should be at most %{file_size})
  number:
    human:  
      storage_units:
         format: "%n %u"
      units:
         byte:
           one: "Byte"
           other: "Bytes"
         kb: "KB"
         mb: "MB"
         gb: "GB"
         tb: "TB"

这里是video.rb

require 'file_size_validator'
class video < ActiveRecord::Base
mount_uploader :file, VideoUploader
before_save :update_file_attributes, :validatefile
before_update :update_file_attributes

validates :file,
  presence: true,
  :file_size => { 
      :maximum => 20.megabytes.to_i 
    }

这是videos_controller.rb 第 67 行

@video = Video.create(params[:video])

您能帮我更正我的步骤和代码吗?

谢谢

【问题讨论】:

    标签: ruby-on-rails validation ruby-on-rails-3.2 carrierwave


    【解决方案1】:

    您的翻译文件中有嵌套错误。应该是

    id:
      errors:
        messages:
          wrong_size: "is the wrong size (should be %{file_size})"
          size_too_small: "is too small (should be at least %{file_size})"
          size_too_big: "is too big (should be at most %{file_size})
      number:
        human:  
          storage_units:
            format: "%n %u"
            units:
               byte:
                 one: "Byte"
                 other: "Bytes"
               kb: "KB"
               mb: "MB"
               gb: "GB"
               tb: "TB"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-02
      • 2011-11-23
      • 2013-12-17
      • 1970-01-01
      • 2014-01-06
      • 2011-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多