【问题标题】:Randomize Paperclip image name Rails随机化回形针图像名称 Rails
【发布时间】:2013-09-18 01:16:09
【问题描述】:

嗨,我正在使用本教程来实现图像的随机名称 http://trevorturk.com/2009/03/22/randomize-filename-in-paperclip/

问题是我得到这个错误。

   NameError in RegistrationsController#create

undefined local variable or method `image_file_name' for #<Player:0xbd66ae0>
Rails.root: /srv/www/myfootballproject.com/mfp

Application Trace | Framework Trace | Full Trace
app/models/player.rb:143:in `randomize_file_name'
app/models/user.rb:29:in `is_a_player?'

这是导致问题的模型部分

这是模型中 player.rb 上“播放器类”中的部分视图。

has_attached_file :avatar, :styles => { :profile => "300x300", :thumb => "100x100#"},
    :url  => "/assets/people/:id/:style/:basename.:extension",
    :path => ":rails_root/public/assets/people/:id/:style/:basename.:extension"

    before_create :randomize_file_name

    validates_attachment_size         :avatar, :less_than    => 2.megabytes # Solo aceptar imágenes menores a 2 Mb.
    validates_attachment_content_type :avatar, :content_type => ['image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png', 'image/gif']

  private

    def randomize_file_name
      extension = File.extname(image_file_name).downcase
      self.image.instance_write(:file_name, "#{ActiveSupport::SecureRandom.hex(16)}#{extension}")
    end


    def defeated?
      t = Time.now - created_at

      mm, ss = t.divmod(60)
      hh, mm = mm.divmod(60)
      dd, hh = hh.divmod(24)

      dd > 180 ? true : false
    end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 gem paperclip


    【解决方案1】:

    对于任何寻找这个,。不要打破你的头 XD 这已经在 Paperclip 中完成了

    has_attached_file :avatar, :styles => { :profile => "300x300", :thumb => "100x100#"},
        :url  => "/assets/people/:id/:style/:basename.:extension",
        :path => ":rails_root/public/assets/people/:id/:style/:hash.:extension",
        :hash_secret => "longSecretString"
    

    这里有所有信息

    https://github.com/thoughtbot/paperclip#uri-obfuscation

    【讨论】:

      猜你喜欢
      • 2014-02-11
      • 1970-01-01
      • 1970-01-01
      • 2014-11-14
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-23
      • 1970-01-01
      相关资源
      最近更新 更多