Set Path for Original Images Using Paperclip in Rails

Set path for original images using paperclip in Rails?

I would recommend using a custom interpolation that will place your original files outside the public directory. Something like this:


Paperclip.interpolates :maybe_public do |attachment, style|
style == :original ? "private" : "public"
end

has_attached_file :image, :path => ":rails_root/:maybe_public/:attachment..."

This will save your :original files in a non-publicly accessible directory for protection, but still allow Paperclip access. And it will keep your thumbnails in the public directory for standard access.

How to use original image url in paperclip

<%= photo.attachment.url(:original) %>

That will give you the path to the original file, you can also get the other styles if needed:

<%= photo.attachment.url(:thumbnail) %>
<%= photo.attachment.url(:profile_gallery) %>

Rails and Paperclip storing images in a specific path sets wrong URL

You will have to set URL option:

for me it was:

  has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>", :small=>"60x60>" },
:path => ':rails_root/public/system/:class/:id/:style/:filename',
:url => '/system/:class/:id/:style/:filename'

Not sure for your case as you store images in the app folder directly so you may try(test it from console and modify it):

:path => "/opt/www/myapp/images/:class/:attachment/:id_partition/:style/:filename",
:url => '/images/:class/:attachment/:id_partition/:style/:filename'

Paperclip change default path and hash image names

In your model, you can set the default path, styles and url as so:

has_attached_file :avatar,
:styles => { :large => "500x500>", :medium => "300x300>", :thumb => "100x100>" },
:path => ":rails_root/public/images/:id/:style/:filename",
:url => "/images/:id/:style/:filename"

Paperclip, set path outside of rails root folder

The options ":path" and ":url" must be used together in your case. I believe what is missing is some configurations on your webserver and the :url configuration.

":path" --> tells paperclip where the files are inside the server file system.

":url" --> tells paperclip how to determine the url to be generated. It is relative to the website URL.

So through configurations on your WebServer you should map the server folder where ":path" to a virtual directory under the rails app folder structure.

This virtual directory should than reflect the configuration in the ":url" option.

for example lets say you did

:path => "/tmp/shared/:class/:id_partition/:style.:extension"

Step one configure a virtual folder under your rails app with the name:

MyNewVirtualFolder

and point it to "/tmp/shared"

Step two configure

:url => "/MyNewVirtualFolder/:class/:id_partition/:style.:extension"

and finally re-start your rails app.

paperclip where to place the missing.png default image?

For this line of code:

Paperclip::Attachment.default_options[:default_url] = "/images/default_image.png"

Paperclip is looking for images under /public/images/default_image.png

update:

so you have defined style big and thumb. Paperclip will look for img in public/images/thumb/default_image.png or public/images/big/default_image.png depending what style will you call in <% image_tag @model.image.url(:style) %>.

update #2 (according to update #4 from author's question)

gotcha! If you want model to use :default_url don't send :image in params. remove image from params and lets see how it goes`



Related Topics



Leave a reply



Submit