使用paperclip显示上传的图像

时间:2015-12-26 15:47:23

标签: ruby-on-rails paperclip

文件上传后,它会以其真实名称.ext保存在数据库中,但在上传的目录中使用不同的名称-cryptic.ext。我正在尝试显示上传的图片@user.legal_docs.url,但它不呈现任何内容。我检查了我的网络工具栏,发现它在正确的目录中引用但文件名不正确,这是其他的 - 没有扩展名的神秘内容。

db = xyz.jpg中的原始文件名

上传的文件dir = something-cryptic.ext

发出@user.legal_docs.url = something-else-cryptic

时的

图片名称

User.rb

class User < ActiveRecord::Base
  has_many :identities, dependent: :destroy
  has_attached_file :legal_docs,
                    url: '/system/:hash.:extension',
                    hash_secret: 'abc123'

  validates_attachment :legal_docs,
                       content_type: { content_type: ['image/jpeg', 'image/gif', 'image/png'] },
                       size: { in: 0..1024.kilobytes }
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :omniauthable, :invitable, :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable
  def facebook
    identities.where(provider: 'facebook').first
  end

  def facebook_client
    @facebook_client ||= Facebook.client(access_token: facebook.accesstoken)
  end

  def twitter
    identities.where(provider: 'twitter').first
  end

  def twitter_client
    @twitter_client ||= Twitter::REST::Client.new do |config|
      config.consumer_key        = ENV['TWITTER_APP_ID']
      config.consumer_secret     = ENV['TWITTER_APP_SECRET']
      config.access_token        = twitter.accesstoken
      config.access_token_secret = twitter.secrettoken
    end
  end
end

verification_controller

def upload
      redirect_to client_verification_path, alert: 'No File Selected' unless params[:user]
      @user = User.find_by(id: current_user.id)
      if @user.update(photo_params)
        flash[:success] = 'Your legal document has been sent for verification. We will contact you soon'
        redirect_to client_dashboard_path
      else
        render 'index'
      end
    end

    private

    def photo_params
      params.require(:user).permit(:legal_docs) if params[:user]
    end

1 个答案:

答案 0 :(得分:1)

呈现文件的链接应该是这样的:

<%= link_to "View file", @user.legal_docs.url(:original) %>