使用回形针显示上传的图像
show uploaded image using paperclip
文件上传后,它以真实的 name.ext 保存在数据库中,但在上传目录中使用不同的名称 something-cryptic.ext 。我正在尝试显示上传的图像,@user.legal_docs.url
但它没有呈现任何内容。我检查了我的网络工具栏,发现它在正确的目录中引用,但文件名不正确,它是一些没有扩展名的神秘东西。
数据库中的原始文件名 = xyz.jpg
上传目录中的文件 = 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
渲染文件的 link 应该是这样的:
<%= link_to "View file", @user.legal_docs.url(:original) %>
文件上传后,它以真实的 name.ext 保存在数据库中,但在上传目录中使用不同的名称 something-cryptic.ext 。我正在尝试显示上传的图像,@user.legal_docs.url
但它没有呈现任何内容。我检查了我的网络工具栏,发现它在正确的目录中引用,但文件名不正确,它是一些没有扩展名的神秘东西。
数据库中的原始文件名 = xyz.jpg
上传目录中的文件 = 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
渲染文件的 link 应该是这样的:
<%= link_to "View file", @user.legal_docs.url(:original) %>