【问题标题】:Rails]Carrierwave With OmniAuthRails]Carrierwave 与 OmniAuth
【发布时间】:2012-12-23 08:53:46
【问题描述】:

我的应用使用carrierwave 和omniauth (facebook),但我遇到了一个小问题。

  1. 当用户第一次使用 Facebook 帐户登录时,我将他的 Facebook 个人资料图片设为他的默认图片。 (他不需要注册 FB 帐户或其他任何东西。当他登录时,他已经注册了我的应用程序,而没有提供任何更多信息)
  2. 当用户首次从我的应用程序“本地”创建帐户时,他可以选择上传个人资料图片。因此,在 User 模型中有一个profile_image 字段,并且在该字段上挂载了来自carrierwave 的ProfileUploader

问题:当我将 facebook 图片 url 保存到这个字段时,它给了我错误。我认为这是因为有一个上传器安装到这个字段,虽然这个字段是一个 字符串类型,如果不通过carrierwave上传图片,我不能只保存一个URL到这个字段。

我通过将 FB 图片 url 存储到另一个字段 other_profile_image 来解决此问题,并且每当从 FB 创建用户帐户时,我都会从该字段调用他的图片。

例如,

<% if @user.provider == "local" %>
        <% if @user.profile_image.blank? %>
            <%= link_to image_tag('default.jpg'), '/users/'+@user.id.to_s %>
        <% else %>
            <%= link_to image_tag(@user.profile_image_url(:thumb)), '/users/'+@user.id.to_s %>
        <% end %>
<% elsif @user.provider == "facebook" %>
        <%= link_to image_tag(@user.other_profile_image), '/users/'+@user.id.to_s %>
<% end %>

provider=local specifies accounts created on the application, not from other third-parties like FB

我的解决方法确实有效,但它困扰着我,因为我知道必须有更好的解决方案来处理这个问题。我需要了解如何处理以下简单任务:

  1. 当用户在本地注册时,他可以选择上传个人资料图片。如果他没有,我想将我的默认图片的 url 存储到他的个人资料图片 url 字段中。
  2. 当用户使用他的 FB 帐户时,他的 facebook 图片应该设置为他的初始头像。现在,我正在使用两个不同的字段,profile_imageother_profile_image,因为当我尝试将 URL 字符串存储到 profile_image 字段中时,carrierwave 引发了错误,在该字段上安装了carrierwave 上传器。

我希望有一个很好的方法来解决这个问题!我提前感谢任何帮助。非常感谢。

【问题讨论】:

    标签: ruby-on-rails omniauth carrierwave


    【解决方案1】:

    您可以从远程网址上传照片。

    class User < ActiveRecord::Base
      attr_accessible :profile_image, :remote_profile_image_url
    
      mount_uploader :profile_image, ProfileUploader
    end
    
    
    # somethere
    user.remote_profile_image_url = "http://example.com/userpic.png"
    user.save
    

    【讨论】:

    • 完全忘记了这一点。谢谢!!
    猜你喜欢
    • 1970-01-01
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多