Omniauth Facebook头像

时间:2015-02-19 02:45:08

标签: ruby-on-rails facebook omniauth omniauth-facebook

试图从Facebook上取下头像,但似乎没有任何东西加载。我想我正确访问了Auth Hash。搜索没有提出太多。

User.rb

def self.find_for_oauth(auth, signed_in_resource = nil)

# Get the identity and user if they exist
identity = Identity.find_for_oauth(auth)

# If a signed_in_resource is provided it always overrides the existing user
# to prevent the identity being locked with accidentally created accounts.
# Note that this may leave zombie accounts (with no associated identity) which
# can be cleaned up at a later date.
user = signed_in_resource ? signed_in_resource : identity.user

# Create the user if needed
if user.nil?

  # Get the existing user by email if the provider gives us a verified email.
  # If no verified email was provided we assign a temporary email and ask the
  # user to verify it on the next step via UsersController.finish_signup
  email_is_verified = auth.info.email && (auth.info.verified || auth.info.verified_email)
  email = auth.info.email if email_is_verified
  user = User.where(:email => email).first if email
  avatar = auth.info.image

  # Create the user if it's a new registration
  if user.nil?
    user = User.new(
      name: auth.extra.raw_info.name,
      username: auth.extra.raw_info.name || auth.uid,
      email: email ? email : "#{TEMP_EMAIL_PREFIX}-#{auth.uid}-#{auth.provider}.com",
      avatar: auth.info.image,
      password: Devise.friendly_token[0,20]
    )
    user.skip_confirmation!
    user.save!
  end
end

部分

<%= link_to user_path(user) do %>
    <%= image_tag user.avatar.url || 'avatar.jpg', class: klass %>
<% end %>

思想?

1 个答案:

答案 0 :(得分:0)

如果avatar是一个字符串,那么您认为这会怎么样? user.avatar.url当然不会。如果user.avatar是字符串,则它没有url方法。

只需使用<%= image_tag user.avatar %>

相关问题