检查用户是否提供了图像?

时间:2016-03-18 11:50:10

标签: ruby-on-rails ruby

我正在使用这些宝石来处理我的图像上传器:

gem 'carrierwave'
gem 'mini_magick'
gem 'fog'

但是我似乎无法确定图像是否为空或者为空,因为它不能是因为当我调用@ user.picture时它会返回:

  

“********@hotmail.co.uk”,encrypted_pa​​ssword:

     

“*************”,   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,用户名:“StormViper”,登录名:nil,cart_id:   1,admin:true,picture:“download.jpg”,brand_id:1,is_brand?:   true&gt ;, @ mounted_as =:picture,   @storage =#>中   @ file =#,@ _versions = {}>

这是我的用户控制器:

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异常:未定义的方法`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

2 个答案:

答案 0 :(得分:0)

看起来您确实在用户的图片栏中保存了一些内容。如果您正在寻找布尔值,请尝试@user.picture.present?,如果您正在寻找实际文件@user.picture.url

在Carrierwave上传模型上修改:#url,而不是#file。我和家里的工作混淆了一秒钟!

答案 1 :(得分:0)

您将通过

获取用户图片网址
@user.picture_url and check @user.picture_url.present?