Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get absolute URL for paperclip attachment

Is it possible to get the absolute URI for a Paperclip attachment? Right now, the problem is that the production environment is deployed in a sub-URI (on Passenger: RackBaseURI), but <paperclip attachment>.url returns the Rails-app relative URI (/system/images/...). Is there a way to get the absolute URI for Paperclip attachments?

I'm using Paperclip v2.7 and Rails 3.2.8.

like image 864
sleepy_keita Avatar asked Sep 09 '25 12:09

sleepy_keita


2 Answers

asset_url(model.attachment_name.url(:style))

Relevant github issue

like image 129
Chris Bosco Avatar answered Sep 11 '25 10:09

Chris Bosco


try

URI.join(request.url, @model.attachment_name.url)

or

URI(request.url) + @model.attachment_name.url

It's safe if you use S3 or absolute url.

Update: this answer is better than mine ;) https://stackoverflow.com/a/21027839/683157

like image 29
kuboon Avatar answered Sep 11 '25 10:09

kuboon