问题
I use for loading files gem dragonfly
I have model Photo:
class Photo < ApplicationRecord
dragonfly_accessor :avatar do
default 'public/avatar.png'
end
................................
end
When i call pry(#<#<Class:0x007f83508fb8f0>>)> @photo.avatar.url
I get
=> "/media/W1siZmYiLCJwdWJsaWMvYXZhdGFyLnBuZyJdXQ/avatar.png?sha=338938a0ea07d56b"
Setting file dragonfly.
dragonfly.rb
require 'dragonfly'
# Configure
Dragonfly.app.configure do
plugin :imagemagick
secret "7c510e8ed95f9929f09981ef5f42ffc75034703184575044d2835ebbbd2a350b"
url_format "/media/:job/:name"
datastore :file,
root_path: Rails.root.join('public/system/dragonfly', Rails.env),
server_root: Rails.root.join('public')
end
# Logger
Dragonfly.logger = Rails.logger
# Mount as middleware
Rails.application.middleware.use Dragonfly::Middleware
# Add model functionality
if defined?(ActiveRecord::Base)
ActiveRecord::Base.extend Dragonfly::Model
ActiveRecord::Base.extend Dragonfly::Model::Validations
end
I need path image for paste in image_tag. How can I get correct path .../public/avatar.png?
回答1:
I founded solution:
dragonfly_accessor :avatar do
default Rails.root.join('public', 'images', 'default', 'avatar.png')
end
回答2:
Try this
img = Dragonfly[:images].fetch_file(File.join(Rails.root, 'public', 'toekomst', 'images', 'sample.png'))
refer this link (http://markevans.github.io/dragonfly/rails/)
回答3:
You should use remote_url instread of url to get full path like
@photo.avatar.remote_url
If you want to use the uploaded image in image tag, then you can directly use
image_tag(@photo.avatar.url)
来源:https://stackoverflow.com/questions/36442304/how-get-correct-path-default-image-using-gem-dragonfly