0

I'm trying to pull in user's profile avatars from twitter using these codes

Any idea how I need to fix this error message?

Here's what I'm using in the Users Helper

module UsersHelper

def twitteravatar_for(user, options = { size: 50 })
    omniauth = request.env['omniauth.auth']
    self.image = omniauth['info']['image'].sub("_normal", "_bigger")
    gravatar_url = self.image
    image_tag(gravatar_url, alt: user.name)
  end
end

Then I'm trying to use this in view and pull the user's individual avatar

<%= link_to twitteravatar_for(feed_item.user), feed_item.user %>

Is it because I can't use request.env['omniauth.auth'] in helper?

4

2 回答 2

0

一个很好的检查方法是

img = request.env.try('[]','omniauth.auth').try('[]','info').try('[]','image')

如果中间出现问题,答案是正确的 img 或 nil。

于 2013-04-14T03:25:43.677 回答
0

链的一个值将为零

request.env['omniauth.auth']['info']['image']

使用 puts 来确定是哪一个

puts request.env['omniauth.auth']

例如,omniauth['info']['image'] 可能是 nil,因为用户没有图像集?

于 2013-04-14T03:13:19.320 回答