3

関数変数をシンボルとして搬送波に渡そうとしていますが、どうすればこれを正しく行うことができますか?

そのようです:

    image_tag @profile.photos.first.file_url(:size)

私の機能:

  # Get avatar in correct size
  # Display or return default image
  def get_avatar(id, size)

    this_size = size.to_sym
    @profile = User.find(id).profile rescue nil
    image_tag @profile.photos.first.file_url(this_size)

  rescue
    image_tag ("/assets/avatars/img_#{size}.png")
 end
4

1 に答える 1

11

サイズが文字列でない場合は、最初に変換する必要があります。

これを試して:

this_size = size.to_s.to_sym

または、その行を完全にスキップして、これを使用します。

@profile.photos.first.file_url(:"#{size}")
于 2012-07-07T06:17:52.473 に答える