私は最初の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>
私は何が間違っているのですか?