1

嗨,我期待解决这个独特的图像名称问题:当用户上传图像(每个用户都有自己的文件夹)并且图像已经存在时,我会收到消息。

Errno::EACCES in PlayerStepsController#update

Permission denied - /srv/www/myfootballproject.com/mfp/public/assets/people/14/original/chepo.jpg
Rails.root: /srv/www/myfootballproject.com/mfp

Application Trace | Framework Trace | Full Trace
app/controllers/player_steps_controller.rb:30:in `update'
Request

我查看了stackoverflow,首先我阅读了有关为文件夹设置用户标识的信息,这可以最大限度地减少同名的概率,我也尝试随机化,但这根本不起作用,我没有错误,也没有随机化图像名称..

这是执行此操作的模型的一部分。

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

  private

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

这是模型(完整)

class Player < ActiveRecord::Base

  belongs_to :user
  has_many   :clubs
  has_many   :links
  has_many   :references
  has_many   :achievements
  has_many   :citizens
  has_and_belongs_to_many :languages
  has_and_belongs_to_many :selections

  accepts_nested_attributes_for :clubs,        :allow_destroy => true, :reject_if => proc { |attributes| attributes['name'].blank? }
  accepts_nested_attributes_for :links,        :allow_destroy => true, :reject_if => proc { |attributes| attributes['url'].blank? }
  accepts_nested_attributes_for :references,   :allow_destroy => true, :reject_if => proc { |attributes| attributes['name'].blank? }
  accepts_nested_attributes_for :achievements, :allow_destroy => true, :reject_if => proc { |attributes| attributes['name'].blank? }
  accepts_nested_attributes_for :citizens,     :allow_destroy => true, :reject_if => proc { }

  attr_accessible :name,
    :lastname,
    :birthday,
    :height,
    :height_measure,
    :weight,
    :weight_measure,
    :inches,
    :city,
    :birthplace,
    :other_languages,
    :cp,
    :phone,
    :cellphone,
    :web_page,
    :game_status,
    :club,
    :actual_club,
    :actual_country_club,
    :actual_division_club,
    :actual_contract_expiration_club,
    :last_club,
    :last_country_club,
    :last_division_club,
    :last_contract_expiration_club,
    :position,
    :alternative_position,
    :dominant_leg,

    #normal player
    :short_passes,
    :long_passes,
    :shots_half_distance,
    :shots_long_distance,
    :ball_habilities,
    :offensive_capability,
    :ball_driving,
    :defense_capability,
    :dribbling,
    :velocity,
    :vision_field,
    :movements_wothout_ball,
    :recovery_ball,
    :head_ball,
    :lidership,
    :teamwork,

    #goalkeeper
    :air_game,
    :clearance_technique, #técnica de despeje
    :ball_keep, #atajes
    :flexibility, #flexibilidad
    :penalty_keep, #atajar penales
    :achique,
    :defense_communication,
    :foot_game,
    :velocity_reaction, #reflejos
    :area_domination,
    :goalkeep_teamwork,
    :goalkeep_lidership,

    :strenghts,
    :weaknesses,

    :aditional_information,

    :active,

    :clubs_attributes,
    :links_attributes,
    :references_attributes,
    :achievements_attributes,
    :citizens_attributes,
    :avatar_file_name,
    :avatar_content_type,
    :avatar_file_size,
    :avatar,

    :language_ids,
    :selection_ids


    POSITIONS = %w{
      goalkeeper
      defense
      medium
      offensive
    }

    LEG = %w{
      left
      right
      both
    }

    # altura
    HEIGHT = (1..200).to_a

    INCH = (1..11).to_a

    # peso
    WEIGHT = (1..300).to_a

    HEIGHT_MEASURE = %w{
      cms
      pies
    }

    WEIGHT_MEASURE = %w{
      kgs
      lbs
    }

    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

  private

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

    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']


    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
end

一切正常,我可以上传图片(只要它们不存在于服务器中)

谢谢

4

0 回答 0