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
Generate and Publish Ruby Based Rest APIs Documentation
Ruby Mixins: Extend and Include
How to Select Option in Drop Down Using Capybara
Ruby: How to Define a Class Method in a Module
Adding Attributes to a Ruby Object Dynamically
Error Loading Rubygems Plugin ,Openssl.Bundle (Loaderror)
Only Show Decimal Point If Floating Point Component Is Not .00 Sprintf/Printf
Jekyll on Windows: Pygments Not Working
Rails 3 - Restricting Formats for Action in Resource Routes
Rails Rbenv: Rails: Command Not Found
Adding a Staging Environment to the Workflow
How to Install Ruby on Rails 3 on Osx
How to Manage Multiple Gemsets and Ruby Versions with Rvm
Reraise (Same Exception) After Catching an Exception in Ruby
Is There an Add_Days in Ruby Datetime
Paypal Website Payments Standard with a Ruby/Rails Application