跳过默认图像回形针

时间:2018-02-20 19:40:49

标签: ruby-on-rails paperclip

我想跳过我分配给paperclip的默认图像,只有当remote_avatar不为null时,也就是说,如果用户启动与使用remote_avatar的facebook的会话,而在相反的情况下,paperclip将默认图像分配给我附件:avatar

user.rb

# == Schema Information
#
# Table name: users
#
#  id                     :integer          not null, primary key
#  email                  :string           default(""), not null
#  encrypted_password     :string           default(""), not null
#  reset_password_token   :string
#  reset_password_sent_at :datetime
#  remember_created_at    :datetime
#  sign_in_count          :integer          default(0), not null
#  current_sign_in_at     :datetime
#  last_sign_in_at        :datetime
#  current_sign_in_ip     :string
#  last_sign_in_ip        :string
#  confirmation_token     :string
#  confirmed_at           :datetime
#  confirmation_sent_at   :datetime
#  username               :string
#  avatar_file_name       :string
#  avatar_content_type    :string
#  avatar_file_size       :integer
#  avatar_updated_at      :datetime
#  bio                    :text
#  birthdate              :date
#  nationality            :string
#  gender                 :string
#  created_at             :datetime         not null
#  updated_at             :datetime         not null
#  roles_mask             :integer
#  uid                    :string
#  provider               :string
#  doctor_id              :integer
#  facebook               :string
#  remote_avatar          :string
#
# Indexes
#
#  index_users_on_confirmation_token    (confirmation_token) UNIQUE
#  index_users_on_doctor_id             (doctor_id)
#  index_users_on_email                 (email) UNIQUE
#  index_users_on_reset_password_token  (reset_password_token) UNIQUE
#  index_users_on_username              (username) UNIQUE
#
class User < ApplicationRecord
  ...

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:facebook]

  validates :username, uniqueness: {message: "The username has already been taken."}

  has_attached_file :avatar, styles:{thumb:"42x42",medium:"300x300"}, default_url:"/images/:style/user.png"
  validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/

  def self.from_omniauth(auth)
    where(provider: auth[:provider], uid: auth[:uid]).first_or_create do |user|
      if auth[:info]
        ...

        user.facebook = auth[:extra][:raw_info][:link]
        if auth[:info][:image]
          user.remote_avatar = auth[:info][:image]
          user.avatar = nil
        end

        ...
      end
    end
  end
end

1 个答案:

答案 0 :(得分:1)

看起来你可以更容易:

<%= current_user.avatar? ? current_user.avatar.url(:medium) : '/images/medium/user.png' %>

或者您可以使用帮助器,例如:

def self.fetch_avatar(user)
  return '/images/medium/user.png' unless user.avatar?
  user.avatar.url(:medium)
end

ERB:

<%= SomethingHelper.fetch_avatar(current_user) %>
相关问题