简体   繁体   中英

rails 3 - paperclip

With paperclip, how can you get paperclip to use full urls like http://mysite.com/image/asdasd.png

versus /image/asdad.png

thanks

Once you configure the :url interpolation string to your satisfaction, you can link to attachments with the full URL using something like:

def attachment_path(attachment)
  attachment.url
end

def attachment_url(attachment)
  "#{root_url}#{attachment.url.gsub(/^\//, '')}"
end

Assuming you want the behavior to work like the _url helpers on the controller/view level, its a little complicated as paperclip functions without the benefit of knowing the host from the request. An easy way to get around this is to define the constant HOST in the config/environments/ENV.rb and then passing the url parameter to has_attachment like

:url => "http://#{HOST}/:path"

or whatever your url rules are.

You can also side step this issue by using S3, which is kind of a life saver

If you are uploading files to amazon S3 then

s3.url
gives the full image path. But in case of local file storage, you can set :url option also

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM