我正在使用这些 gem 来处理我的图片 uploader :
gem 'carrierwave'
gem 'mini_magick'
gem 'fog'
但是我似乎无法确定图像是否为 nil 或为空,因为它不可能是因为当我调用 @user.picture 时它会返回:
"********@hotmail.co.uk", encrypted_password:
"************************************", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 6, current_sign_in_at: "2016-03-18 11:23:00", last_sign_in_at: "2016-03-18 11:18:16", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2016-03-17 15:00:46", updated_at: "2016-03-18 11:24:13", provider: nil, uid: nil, username: "StormViper", login: nil, cart_id: 1, admin: true, picture: "download.jpg", brand_id: 1, is_brand?: true>, @mounted_as=:picture, @storage=#>, @file=#, @versions={}>
这是我的用户 Controller :
if @user.save
p "USER SAVED LOGIN: #{@user.username}"
@cart = Cart.create(:user_id => @user.id, :cart_count => 0)
@cart.save
@user.cart_id = @cart.id
@user.save
@slot = Slot.create(:cart_id => @cart.id)
@slot.save
@cart.slot_id = @slot.id
@cart.save
redirect_to root_path
else
p "USER FAILED LOGIN: #{@user.username}"
render 'new'
end
我该如何解决这个问题?我无法直接更改返回的 picture.url:
*** NoMethodError Exception: undefined method `url=' for
PictureUploader:0x007f73e6135e78>
我的用户模型包含:
class User < ActiveRecord::Base
has_one :cart
has_many :slots, through: :carts
belongs_to :brand
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
attr_accessor :login
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:authentication_keys => [:login]
validates :username, presence: true, uniqueness: true
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions.to_hash).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
elsif conditions.has_key?(:username) || conditions.has_key?(:email)
where(conditions.to_hash).first
end
end
mount_uploader :picture, PictureUploader
end
最佳答案
看起来您确实在用户的图片列中保存了一些内容。如果你正在寻找一个 bool 值,我会尝试 @user.picture.present?
,如果你正在寻找实际的文件,@user.picture.url
.
编辑:#url
,而不是 Carrierwave 上传模型上的#file
。我有那么一瞬间把工作和家搞混了!
关于ruby-on-rails - 检查用户是否提供了图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36083608/