user1049097
user1049097

Reputation: 1901

Paperclip giving wrong url. How to fix?

My paperclip path in the model is:

has_attached_file :image, :path => ":rails_root/app/assets/images/article_images/:id/:style_:basename.:extension"

But when I call article.image.url, here is the url I get (which is broken):

/system/images/64294/original/fantastik.jpg?1324288670

Upvotes: 2

Views: 3560

Answers (3)

mirap
mirap

Reputation: 1266

This is how to fix the issue with :default_url:

:default_url => ActionController::Base.helpers.asset_path('empty-event-cover.png')

Upvotes: 0

lisowski.r
lisowski.r

Reputation: 3761

like @andrewpthorp mention that's because you switch only path, but you can do it more DRY

paperclip defaults:

:url                   => "/system/:attachment/:id/:style/:filename",
:path                  => ":rails_root/public:url",

You can see that url can be part of path so your config should look like:

 has_attached_file :image,
   :url => "/app/assets/images/article_images/:id/:style_:basename.:extension",
   :path => ":rails_root:url"

Be carefull, usualy servers (apache, nginx) serves files only from public directory.

More options for has_attached_file you can find here

Upvotes: 0

andrewpthorp
andrewpthorp

Reputation: 5106

You also need to setup the URL, what you want is:

has_attached_file :image,
                   :path => ":rails_root/app/assets/images/article_images/:id/:style_:basename.:extension"
                   :url => "/app/assets/images/article_images/:id/:style_:basename.:extension"

The only thing I would question is if you're storing them in a good place. Typically, they would go in

/public/...

Or on another service like s3. However, that is how you modify the URL

Hope this helps!

Upvotes: 6

Related Questions