0

私は最初のRoRアプリを作成しており、現在、ユーザーが画像をアップロードできるように取り組んでいます。私はこの目的のためにペーパークリップを使用しています。手順の1つはhas_attached_file、モデルに追加することです。

class MyModel < ActiveRecord::Base
  #...
  has_attached_file :picture, styles: {
    large: "120x150#>", 
    small: "60x75#>"
  }
  #...
end

このようにすれば、すべてがスムーズに機能します(またはそう思われます)。ただし、他の場所で整数と同じ定数値にアクセスする必要があるため、ハッシュを追加しました。

class MyModel < ActiveRecord::Base
  #...
  has_attached_file :picture, styles: {
    large: "120x150#>", 
    small: "60x75#>"
  }
  def picture_sizes
  {
    large: {width: 120, height: 150},
    small: {width: 60, height: 75}
  }
  end
  #...
end

これにより、醜い冗長性が生まれます。そこで、次のように、2番目のハッシュから最初のハッシュを生成するメソッドを作成することを考えました。

class MyModel < ActiveRecord::Base
  #...
  has_attached_file :picture, styles: picture_sizes_as_strings

  def picture_sizes
  {
    large: {width: 120, height: 150},
    small: {width: 60, height: 75}
  }
  end

  def picture_sizes_as_strings
    result = {}
    picture_sizes.each do |label, size|
      result[:label] = "#{size[:width]}x#{size[:height]}#>"
    end
    return result
  end
  #...
end

しかし、これはエラーを引き起こします:

undefined local variable or method `picture_sizes_as_strings' for #<Class:0x007fdf504d3870>

私は何が間違っているのですか?

4

2 に答える 2

1

実行時にhas_attached_file評価されます。インスタンスメソッドを定義しましたが、インスタンスコンテキストからメソッドを呼び出していません。

試す:

def self.picture_sizes_as_strings
  # code here
end

self.他のメソッドも必ず定義してください

その後:

has_attached_file :picture, :styles => picture_sizes_as_strings

動作するはずです。

于 2012-08-26T19:12:06.503 に答える
1

問題は、クラスレベルで実行されるpicture_sizes_as_strings宣言( )でインスタンスメソッドを使用しようとしていることです。has_attached_imageそれは呼び出すことの違いです

MyModel.picture_sizes_as_strings

MyModel.first.picture_sizes_as_strings

最初のケースでは、クラスメソッド(クラスMyModel自体のメソッド)を参照しており、2番目のケースでは、インスタンスメソッド(個々のmy_modelオブジェクトのメソッド)を参照しています。

したがって、まず最初に、名前の前に。を付けて、メソッドをクラスメソッドに変更する必要がありますself.

def self.picture_sizes
  {
    large: {width: 120, height: 150},
    small: {width: 60, height: 75}
  }
end

has_attached_imageモデルが最初にrubyによって解析されるときに処理されるため、これで問題が完全に解決されるわけではありません。つまり、has_attached_image定義する前に実行を試みるself.picture_sizesため、まだと表示されますundefined method

self.picture_sizes宣言の前に置くことでこれを修正できますhas_attached_fileが、それはかなり醜いです。データを定数に入れることもできますが、それには独自の問題があります。

正直なところ、これを修正するための本当にきれいな方法はありません。私の場合は、おそらくプロセス全体を逆にして、スタイルを通常どおりに定義してから、次のようなメソッドを使用して文字列を整数に変換します。

class MyModel < ActiveRecord::Base
  has_attached_file :picture, styles: {
    large: "120x150#>", 
    small: "60x75#>"
  }

  def numeric_sizes style
    # First find the requested style from Paperclip::Attachment
    style = self.picture.styles.detect { |s| s.first == style.to_sym }

    # You can consolidate the following into one line, I will split them for ease of reading
    # First strip all superfluous characters, we just need the numerics and the 'x' to split them
    sizes = style.geometry.gsub(/[^0-9x]/,'')
    # Next split the two numbers across the 'x'
    sizes = sizes.split('x')
    # Finally convert them to actual integer numbers
    sizes = sizes.map(&:to_i)
  end
end

次にMyModel.first.numeric_sizes(:medium)、配列として返される特定のスタイルのサイズを見つけるために呼び出すことができます。もちろん、それらをハッシュまたは必要な形式に変更することもできます。

于 2012-08-26T19:26:42.123 に答える