Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails回形针结果数组索引_Ruby On Rails_Paperclip - Fatal编程技术网

Ruby on rails Rails回形针结果数组索引

Ruby on rails Rails回形针结果数组索引,ruby-on-rails,paperclip,Ruby On Rails,Paperclip,我尝试从资产中获取化身的方法。My User.rb代码: def get_avatar(size) if self.assets.length != 0 self.assets.each do |asset| if asset.avatar asset.photo.url(size) end end else default_avatar(size) end 结束 鉴于: user.get_avatar(:small) 它应该可以工作,但鉴于我只有以下

我尝试从资产中获取化身的方法。My User.rb代码:

def get_avatar(size)

if self.assets.length != 0
  self.assets.each do |asset|
    if asset.avatar
     asset.photo.url(size)
    end
  end
else
  default_avatar(size)
end
结束

鉴于:

user.get_avatar(:small)
它应该可以工作,但鉴于我只有以下几点:

 <img alt="Jpeg&quot;, photo file size: 121649, photo updated at: &quot;2013 08 20 07:51:42&quot;, user id: 109, active: false, avatar: true&gt;]" src="/images/[#&lt;Asset id: 176, created_at: &quot;2013-08-20 07:51:43&quot;, updated_at: &quot;2013-08-20 07:51:43&quot;, photo_file_name: &quot;user_2.jpg&quot;, photo_content_type: &quot;image/jpeg&quot;, photo_file_size: 121649, photo_updated_at: &quot;2013-08-20 07:51:42&quot;, user_id: 109, active: false, avatar: true&gt;]">
它只返回照片url。但它仍然不起作用

当我这样做时:

self.assets.first.photo.url(size) 

它起作用了。区别是什么?

在您的
get_avatar
函数中,您循环资产数组,当您获得资产的url时,您不返回它,因此函数返回数组本身。因此,当您获得url时添加return

def get_avatar(size)

  if self.assets.length != 0
    self.assets.each do |asset|
      if asset.avatar
        return asset.photo.url(size)
      end
    end
  else
    default_avatar(size)
  end
end

希望有帮助。

你在干什么?self指的是什么?什么是资产?在此处发布包含附件的模型。只有这样,我们才能知道你想做什么?这很简单-如果它的User.rb soo self指的是User model.Ok。所以模型是User.rb。请把它贴在这里。
def get_avatar(size)

  if self.assets.length != 0
    self.assets.each do |asset|
      if asset.avatar
        return asset.photo.url(size)
      end
    end
  else
    default_avatar(size)
  end
end