How to copy a file using Paperclip - ruby-on-rails

Does anyone know of a way to copy files with Paperclip using S3 for storage? Before I try to write my own, I just wanted to make sure there wasn't already a way to do this. Thanks

After some more messing around with paperclip, I figured it out. It's ridiculously simple to copy files!
# Stupid example method that just copies a user's profile pic to another user.
def copy_profile_picture(user_1, user_2)
user_2.picture = user_1.picture
user_2.save # Copied the picture and we're done!
end
This also works great with amazon s3. Sweet

Related

Local file upload: File.open or StringIO

My application allows a user to upload an image and then create different versions of that image (e.g. aligned and cropped with another image). The intermediate step is that I need to copy the uploaded file to another object and process it. The easiest way to do this is to upload it locally.
From the carrierwave wiki, they suggest using a modified version of StringIO.
On the carreirwave readme, they also suggest using File.open (something like obj.image=File.open('path_to_file').
I've also found references to using fixture_file_upload from ActionDispatch::TestProcess (usually in testing, but I'm unsure why it is restricted to that environment).
Can anyone give me a good explanation on the pros and cons (if any) of using these methods?
Thanks.
I just discovered one major difference, at least in the context of carrierwave. If you use carrierwave with the move_to_cache option set to true and mount your upload column with File.open, the file given to File.open will be moved, while with StringIO, it won't.

Carrierwave/fog are uploading files to S3, but the app is still trying to use the local path to access images

I know this is a broad question, and I'm biting off a little more than I can chew for a first stab at a rails app, but here I am.
I tried to add an image upload/crop to a basic status app. It was working just fine uploading the images and cropping them with carrierwave, but as soon as I started using Fog to upload to S3, I ran into issues.
The image, and it's different sizes, appear to be ending up on S3 just fine, but the app is still trying to access the image as "/assets/uploads/entry/image/65/large_IMG_0035.jpg"
Locally, it just shows a broken image, but on Heroku it breaks the whole thing because
ActionView::Template::Error (uploads/entry/image/1/large_IMG_0035.jpg isn't precompiled
The heroku error makes sense to me because it shouldn't be there. I've combed through the app but don't know what's forcing this. I'll post any code anybody thinks will work? Thanks in advance!
Clarification:
Just to clarify, the images are uploading to S3 fine, the problem is how the app is trying to display the image_url
The app is using a local path in the asset pipeline, not the S3 path that it's actually uploading to.
I was having the same issue. In my Carrierwave Initializer I was setting host to s3.amazonaws.com but when I removed that line altogether urls started working.
I hope this helps you resolve your issue, I fought this for several hours!
I believe this issue is related to how you are accessing your image in your view.
If you have mounted an uploader on the field avatar in the following manner:
class User < ActiveRecord::Base
mount_uploader :avatar, AvatarUploader
end
You would access it in your ERB as follows:
<%= image_tag(#user.avatar_url) %>
I would also suggest watching the following Railscast on the topic.
http://railscasts.com/episodes/253-carrierwave-file-uploads
Re-reading issue, I bet it has to do with Carrierwave using Herkou.
Give this a glance and see if it helps.
https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Make-Carrierwave-work-on-Heroku
I am not clear what exactly do you want to achieve.
But for now I have 2 ideas:
For assets host in CDN, you can take a look at this:
https://devcenter.heroku.com/articles/cdn-asset-host-rails31
If you want the images to be part of a model-relation, here's my rough idea:
Put the images path in a table column.
For further information about this you can browse carrierwave github site.(It has many docs and tutorial)

File uploading rails 3

I have a rails 3 app that has a comment system that allows a user to make comments. What I now wish to do is extend this further by enabling the user to attach/upload files to the comment system. Fairly easy question I am about to ask. What is the best tool/plugin or gem to use so that I can achieve this? I've done some thorough research and found that alot of these plugins such as carrierwave, paperclip and a few others are used to upload photos and I am not trying to do this.
Requirements
User should be able to upload/attach files
Can upload word, powerpoint or execel docuements
Both carrierwave and paperclip can be used to upload any type of files - they have extra features if you want to upload images, but they are not restricted to handling just image files.
In the end it's a matter of preference. I found both paperclip and carrierwave to be very good at what they do.
Paperclip
CarrierWave
As for me CarrierWave more modern and nice solution. Have a try.
You can use even dragonfly - https://github.com/markevans/dragonfly/

Uploading/Downloading files - Ruby On Rails system

I'm attempting to create a simple file hosting system using Ruby On Rails - I have a large ammount of the system setup (including the registration of new files, and stuff) however I've realised there is a bit of a problem - I'm unsure how to actually get it so that users can upload and download files.
I assume I'd need some kind of file_link attribute for my file object, but how would people upload and download files to/from the server?
Also (this may be a slightly different topic) - but how would I get the file information such as file size and name (as I need them for the upload)?
Sorry for all my questions - I've don't really deal with file handling a lot so am new to the area.
Thanks In Advance,
Regards,
Joe
You should look at Paperclip gem https://github.com/thoughtbot/paperclip
It is very easy to use and allows to upload files.
Look at Paperclip. It does a lot of the heavs lifting for you: https://github.com/thoughtbot/paperclip
As they said look at paperclip. I just did a app that allows the users to upload and delete files. To get started with paperclip use http://railscasts.com/episodes/134-paperclip
To download files after uploading them with paperclip. I did the following in the controller
def download
upload = Upload.find(params[:id])
send_file upload.uploaded.path,
:filename => upload.uploaded_file_name,
:type => upload.uploaded_content_type,
:disposition => 'attachment'
flash[:notice] = "Your file has been downloaded"
end
My sample file upload app should be of help https://github.com/skillachie/File-Upload-App
Need to fix a few things , but the ability to upload and download files is completely functional.

Image upload in rails

How do I upload images and zip files in RoR? I am a newbie. So please help.
Give me both the view and the controller code example.
Thanks in advance.
Nav,
Try the paperclip plugin, you can read about it here http://www.thoughtbot.com/projects/paperclip
We prefer CarrierWave for image uploads in Rails. Very easy to integrate and very modular.
The following post describes a solution for image uploading using CarrierWave while image transformations are done seamlessly in the cloud. Uploaded images are stored in the cloud and delivered through a CDN.
No need to install RMagick, MiniMagick and ImageMagick.
http://cloudinary.com/blog/ruby_on_rails_image_uploads_with_carrierwave_and_cloudinary
suggest checking out the Railscast for paperclip.
ImageMagick is also pretty cool
I would recommend paperclip, and the patch that allows you to store the content in the database instead of the file system
http://patshaughnessy.net/paperclip-database-storage
the link has the view and controller examples that you are looking for
I prefer carrierwave is better to upload images easily.
Here's github page , and railscasts page for quick start.
One option is attachment_fu. It allows you to save your uploaded file to the filesystem, database, or Amazon S3. It also allows you to select which image processor is used, such as RMagick or Minimagick.
The link provides better code than I could here.

Resources