【问题标题】:Cant upload image from facebook carrierwave无法从 Facebook 载波上传图片
【发布时间】:2017-08-18 16:57:05
【问题描述】:

我目前有一个用户配置文件,允许用户上传图像文件,使用 jQuery 获取裁剪数据,然后将其发送到 CarrierWave/RMagick 进行裁剪/处理。它终于运作良好。

我现在遇到的问题是,如果用户使用 Facebook (oAuth) 注册,我想将他们的个人资料图像默认设置为他们的 Facebook 图像。我希望这是同一个字段 (TalentProfile.profile_image),因为我希望用户能够将其替换为自定义上传或根据需要将其删除。

这是我目前的流程:

  • 用户使用 facebook 注册,用户已创建。
  • TalentProfile 资源在用户下创建
  • 我去创建 TalentProfile 之后添加 profile_image。

这是我的 Talent_profile.rb 模型:

class TalentProfile < ApplicationRecord
    belongs_to :user
    mount_uploader :profile_image, ProfileImageUploader
    validates :profile_image, file_size: { less_than: 5.megabytes }

    def crop_profile_image
        if crop_x.present?
            profile_image.recreate_versions!
        end
    end

    def self.set_facebook_image(profileId, auth)
        @profile = TalentProfile.find(profileId)
        @profile.profile_image = auth.info.image
        byebug
    end
end

运行 self.set_facebook_image 时,似乎无法像这样设置 @profile.profile_image,除非我删除 mount_uploader 并验证行。它总是以 nil 的形式出现,即使 auth.info.image 是我需要的 URL。这让我相信,如果我更新它,CarrierWave 无法像这样处理它;好像需要提交表单。

我怎样才能完成这项工作?是否可以?感谢您提供任何信息!

【问题讨论】:

    标签: ruby-on-rails ruby carrierwave


    【解决方案1】:

    你应该使用remote_profile_image_url方法从远程url上传图片。

    【讨论】:

    • 您是说将@profile.profile_image = auth.info.image 行更改为@profile.remote_profile_image_url = auth.info.image?如果是这样,那似乎不起作用。我必须将该属性添加到我的模型中吗?
    • 德普。我将该行更改为@profile.update(remote_profile_image_url: auth.info.image),它起作用了。你是最好的!谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-01
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    相关资源
    最近更新 更多