【问题标题】:Omniauth Facebook AvatarOmniauth Facebook 头像
【发布时间】:2015-04-20 07:03:07
【问题描述】:

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

用户.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 %>

想法?

【问题讨论】:

  • 头像栏的类型是什么?它应该是一个字符串
  • 它是一个字符串。 t.string
  • 试试 user.avatar.url(:thumb)
  • 厌倦了那些,也没有工作。还附带:(auth.info.image + "?type=large").gsub("­http","htt­ps")

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


【解决方案1】:

如果avatar 是一个字符串,那么你认为这会如何工作? user.avatar.url 当然不会。如果user.avatar 是一个字符串,那么它没有url 方法。

只需使用&lt;%= image_tag user.avatar %&gt;

【讨论】:

    猜你喜欢
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 2016-09-23
    • 2014-05-23
    • 2012-03-05
    • 1970-01-01
    相关资源
    最近更新 更多