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を調べ、最初にフォルダーのユーザーIDを設定することについて読みました。これにより、同じ名前の可能性が最小限に抑えられます。また、ランダム化を試みますが、これはまったく機能しません。エラーも画像のランダム化名も取得しません..

これがこれを行うモデルの部分です。

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