i've been using Paperclip, when deleting is done, it is just deleting the attachments, but the images saved in the file system is not being deleted.can anybody plz tell how to delete the images from the file system using paperclip itself.
the code i've been using is
has_attached_file :image_o_filename,
:styles => {
:tn => ["100x100#",:jpg ,:name => :tn],
:w => ["640x480>",:jpg, :name => :w],
:l_tn => ["200x150#",:jpg, :name => :l_tn]
},
:path => "/places/hotels/:image_pre_path/images/:basename_:style.:extension"
when i delete the photos from the view, images are getting deleted from the database but not from the file system with the above path, images remain without getting deleted. what might be the reason ??
and no errors generated in console also..
Paperclip has a very nice built in delete method, I was stuck on a very similar problem and was very well informed in this solution:
I created a method inside the controller called delete_image then from the view I could call this method.
def delete_image
#user.image.destroy
end
I had the image attached to a user instance and this worked perfect, don't forget to define your method in the routes file in your config/routes.
Good luck also you can look up more information here:
See: Rails Paperclip how to delete attachment?
Related
I use paperclip to upload a users avatar. The image is stored correctly in the /public directory. However I cant figure out how I can get the image displayed. I played with the :url and :path settings for about an hour and cant match them in a way the image will be displayed in the browser.
There is always a 'images/localhost' in the GET-requests path that I can not get rid of.
Here is my code:
user.rb
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "missing.png",
:url => ':class/:id/:style.:extension',
:path => ':url'
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
end
The path in the html-tag looked up by the request looks like this:
<img alt="Original" src="/images/localhost/users/1/original.png?1405249849" />
The correct request which returns the image would be
http://localhost:3000/users/1/original.png?1405248482.
How can I get the request match the correct file-system-path. Or: what is wrong with my settings?
I didnt change the application.rb or the development.rb
Best regards,
Kev
you should take url or path helpers of paperclip wherever possible. So to show the image use:
<%= image_tag #user.avatar.url(:medium) %>
Then:
the image url isn't a file system path. Depending on the storage you use in paperclip, images can reside in different places, see understanding storage of the paperclip gem.
If you use file storage, the files are store somewhere like
public/system/users/avatar/000/000/013/small/my_pic.png
Timestamp
What you're seeing is Paperclip's timestamp - the last time the file / object was updated.
Although I can't find any official reference to this number, it basically allows you to determine which files you're dealing with. According to the question referenced above, it's apparently there for if you want to ensure your visitors see the latest version of the file (IE never gets stored in the cache)
I'm not sure why there is a disparity between your stored image & your path. However, I would say the path is correct; you just need to be able to
--
Bottom line - if your image shows on the page, I don't think there's any systemic issue with your path; if it doesn't show on the page, can you provide logs / reference to the error?
This post, https://stackoverflow.com/a/26222093/1949363, which pointed to http://www.bwigg.com/2009/10/paperclip-customizing-paths-and-urls/ was the answer for me. I was having issues only in my test environment but I believe the fix should work in other environments as well.
Try the following settings:
:path => "public/system/:class/:id/:filename",
:url => "/system/:class/:id/:basename.:extension"
This question already has answers here:
How do you access the raw content of a file uploaded with Paperclip / Ruby on Rails?
(7 answers)
Closed 5 years ago.
I have paperclip running to upload and store files in a non-public directory on the server. Now I want to be able to read the files directly and or feed them into a gem such as axlsx. I'm struggling with even simply looping threw a text file and think I'm missing something basic (as is usually the case).
Here is my first attempt at opening the file:
Paperclip config in application.rb:
config.paperclip_defaults = {:storage => :fog, :fog_credentials => {:provider => "Local", :local_root => "#{Rails.root}/secured_storage"}, :fog_directory => "", :fog_host => "localhost"}
Model:
class Census < ActiveRecord::Base
has_attached_file :censusfile
validates_attachment_content_type :censusfile,
:content_type => ["application/octet-stream", "text/plain"]
end
In the Controller:
def processcensus
#census=Census.find(params[:id])
#file=#census.censusfile.path
end
In the View:
<% File.readlines(#file).read do |line| %>
<%= line %>
<% end %>
This fails because the 'path' returned by Paperclip is the path relative to its sotrage path, not the full path.
UPDATE: If I add the directory (in this case "secured_storage" in from of the path it work as expected. For example:
#file="secured_storage/" + #census.censusfile.path
Not sure if this is at all the way to address this. If it is, is there a way to ask Paperclip where it is storing the files??
I've read where I could use:
Paperclip.io_adapters.for(#census.censusfile).path
But that seems to read the file into an array unless I'm missing something completely. My goal is to be able to loop threw a text file as well as feed an Excel file to axlsx for processing. I would also like to be able to eventually feed these files directly to a user somehow to allow for secure downloads.
I have looked hard for some documentation on all this and haven't found anything that really explains it yet. I'm to that point of just randomly throwing code here or there and hoping something works, which rarely does. Any help/direction that could be provided would be GREATLY appreciated!!!
Mark
I think io adapter can support read
Paperclip.io_adapters.for(#census.censusfile).read
so
<% Paperclip.io_adapters.for(#census.censusfile).read %>
<%= line %>
<% end %>
Use the copy_to_local_file method. This returns a file object on which you can read like on a normal file`.
Let's says I have the following entry in my seeds.rb file :
Image.create(:id => 52, :asset_file_name => "somefile.jpg", :asset_file_size => 101668, :asset_content_type => "image/jpeg", :product_id => 52)
If I seed it, it tries to process the image specified, I get this error :
No such file or directory - {file path} etc...
My images are backed up, so I don't really need to create them; but I need the record though. I can't comment the paperclip directive in my model; then it works; but I guess there might be another solution.
Is there another pattern to follow in order to accomplish it ? Or a turnaround to tell paperclip not to process the image ?
Rather than setting the asset columns directly, try leveraging paperclip and setting it as ruby File object.
Image.create({
:id => 52,
:asset => File.new(Rails.root.join('path', 'to', 'somefile.jpg')),
:product_id => 52
})
The other answer here certainly works for most situations, but in some cases it may be better yet to provide an UploadedFile rather than a File. This more closely mimics what Paperclip would receive from a form and provides some additional functionality.
image_path = "#{Rails.root}/path/to/image_file.extension"
image_file = File.new(image_path)
Image.create(
:id => 52,
:product_id => 52,
:asset => ActionDispatch::Http::UploadedFile.new(
:filename => File.basename(image_file),
:tempfile => image_file,
# detect the image's mime type with MIME if you can't provide it yourself.
:type => MIME::Types.type_for(image_path).first.content_type
)
)
While this code is somewhat more complicated, it has the benefit of correctly interpreting Microsoft Office documents with .docx, .pptx, or .xlsx extensions which, if attached using a File object, will be uploaded as zip files.
This especially matters if your model permits Microsoft Office documents but does not allow zip files, because validations will otherwise fail and your object won't be created. It wouldn't have affected the OP's situation, but it affected mine, and so I wish to leave my solution in case anyone else needs it.
Hope anyone can help on this!
Making use of Paperclip to upload Files to an Application, so two things that i need to cover.
1) What is the best way to link to the File for Download, I am currently linking to the public folder but it is not the best option as it shows the URL which i dont want. Thinking maybe a button_to call be not sure if this is build into Paperclip already. Options.
2) When above is done then the Browser needs to force to download not just open the file, or at least give the user the standard firefox option open or save.
Please Assist Thanks a Mil!!!
I'll answer question NÂș2.
Let's say your model is called gallery.
In your gallery controller you'll add a download method:
def download
#gallery= Gallery.find(params[:gallery_id])
send_file #gallery.gallery_picture.path,
:filename => #gallery.gallery_picture_file_name,
:type => #gallery.gallery_picture_content_type,
:disposition => 'attachment'
end
Now from your routes you'll invoke the root to this method:
match 'gallery/:id' => 'gallery#download', :as => :download
In your views:
- #galleries.each do |gallery|
= link_to 'Download Picture', download_path(gallery.id)
I'm away from home and I can not test this code, but you can visit those questions I did with the same question as yours. Let me know if it solves your problem.
Rails send_file multiple styles from Paperclip, how can I avoid code repetition?
Rails 3.0 associated model with download 'save to' image
In rails, send_file with a parameter set by user is a security hole. More information
In rails 4 you can't just write match, you have to add at the end , via: [:get, :post]. More information
I'm pretty new to Ruby on Rails.
I changed this thread, because i recognized that I was searching for my problems' solution at the wrong end.
Here's my Problem:
I got a Class ProfileProposal which I upload an Image with(Using CarrierWave).
Now I want to convert ProfileProposal to another class, called Profile.
So I pass all the Information to the NEW-Form of Profile.
Works fine with strings, but not with Images.
What I've already tried/done:
Pass the Image as GET Param to the Create Method:
<%= form_for #profile, :url => { :action => "create", :controller => "profiles", :image => #profile_proposal.image } do |f| %>
#
Which now works, so I DO have the image-url.
What's not working is the following:
#profile = Profile.new(params[:profile], :image => new_image_url)
# OR
#profile.image = new_image_url
#profile.image still has the default value given by Carrierwave.
Thanks in advance!
I'm coming from using paperclip, not carrierwave, so I'll try to keep this high level. But I have an idea for you. Maybe you can set the filename of the new attachment before it exists, then move the image to that path. With paperclip this would play out like:
#profile.image_file_name = "profile.jpg"
# creates the directory of the new path. There's probably a better way to do this:
FileUtils.mkdir_p #profile.image_file_path.gsub(/[^\/]*$/,'')
FileUtils.mv #profile_proposal.image_path #profile.image_path
You should embed a hidden field in that Profile form, referencing ProfileProposal by some ID. Then while handling the form server side, after everything is validated and ready for save, you should copy the image using some read/write methods, from ProfileProposal instance to Profile instance. I'm not sure how CarrierWave wants you to do this.
I finally fixed that problem, by using paperclip and creating a new Instance via
Profile.create(:name => #profile_proposal.name, :image => #profile_proposal.image)