我正在开发一个应用程序,您可以通过Facebook登录或注册。 出于这个原因,我有一个可以是Facebook图像或上传图像的图像,但我不知道如何将其放入模型中。
我的模特:
class User < ActiveRecord::Base
attr_accessible :name, :password, :password_confirmation, :big_image, :image, :mini_image
validates_confirmation_of :password
validates_presence_of :password, :on => :create, :unless => :from_facebook
validates_presence_of :name
validates_uniqueness_of :name
unless :from_facebook
mount_uploader :big_image, ImageUploader
end
def from_facebook
self.provider == "facebook"
end
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.big_image = auth.info.image.gsub("=square","=large")
user.image = auth.info.image.gsub("=square","=normal")
user.mini_image = auth.info.image
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.save!
end
end
end
端
我想改变的部分是:
unless :from_facebook
mount_uploader :big_image, ImageUploader
end
因为它不起作用。
答案 0 :(得分:1)
您不希望有条件地调用mount_uploader。
有几种解决此问题的方法:
1)对于使用Facebook注册的用户,允许big_image为nil,如果是这种情况,则从facebook动态获取他们的图片。
2)将他们的facebook图像的大版本保存为big_image。这样您就拥有了所需的所有版本大小,甚至可以让用户稍后覆盖它。