How get correct path default image using gem dragonfly?

◇◆丶佛笑我妖孽 提交于 2019-12-13 07:13:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!