将变量转换为:symbol和pass?

Rub*_*tic 3 symbols ruby-on-rails carrierwave

我试图将函数变量传递给载波作为符号无效,我怎么能正确地做到这一点?

像这样:

    image_tag @profile.photos.first.file_url(:size)
Run Code Online (Sandbox Code Playgroud)

我的功能:

  # 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
Run Code Online (Sandbox Code Playgroud)

Tan*_*ili 10

如果大小不是字符串,则需要先转换.

试试这个:

this_size = size.to_s.to_sym
Run Code Online (Sandbox Code Playgroud)

或者完全跳过该行并使用:

@profile.photos.first.file_url(:"#{size}")
Run Code Online (Sandbox Code Playgroud)