The perfect way to upload file in Rails 3.1 - ruby-on-rails

I'm new in Ruby/Rails, so, my question: what is the perfect way to upload file and save filename in database with rails? Are there any Gem? Or maybe there are good build-in feature?

Also try carrierwave, Ryan has a good screencast about this gem

Just take a look on the links to choose between paperClip & carrierwave :
Rails 3 paperclip vs carrierwave vs dragonfly vs attachment_fu
And
http://bcjordan.github.com/posts/rails-image-uploading-framework-comparison/

You can try Paperclip.It is most popular gem in rails community...
https://github.com/thoughtbot/paperclip
these lines do the all stuff
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end
Try it......

Most people use the Paperclip Gem.

Related

Rails and paperclip i18n

I use Rails 5.1.4 and paperclip. I have i18n website http://example.com/en, http://example.com/de, http://example.com/uk.
How to serve file with different source (img src=?)file name depend on locale?
http://example.com/en/house.jpg
http://example.com/de/haus.jpg
http://example.com/uk/komnata.jpg
Is it possible with paperclip?
How to do it?
Regards
Ssebaaa
You're looking for paperclip interpolations
https://github.com/thoughtbot/paperclip/wiki/Interpolations
This is how you would use them.
In your model that utilizes paperclip, update the path to something of your choice:
has_attached_file :file,
.......
:path => ':locale/:style/:filename'
Then tell paperclip to look for the keyword, and what to replace it with
Paperclip.interpolates :locale do |attachment, style|
attachment.some_logic_on_knowing_what_locale
end
(this part goes in paperclip.rb initializer)

Trimming and Compression the image of paperclip using rake task

I want to trimming the image of paperclip in my database.
Because I`m running my own private web service, but I forgot to add the styles when user create the image...
has_attached_file :avatar,
:storage => :s3,
:styles => { # I forgot to add this styles
:medium => "370x370#", # I forgot to add this styles
:thumbs => "120x120#" # I forgot to add this styles
}
So I want to run the rake task and trimming like 370×370# and 120×120#.
But I cant`t find the way to trimming the image after user save.
Does anyone assist me ?
The paperclip gem comes with a rake task for exactly this purpose:
rake paperclip:refresh:thumbnails
Just run that from the command line and it will take care of generating all of your reduced-size images.

Can't upload zip files using ruby on rails and paperclip gem

I'm working on a rails web application. Just created and prepared some models and stuff. The application is very simple. But now I have a problem by setting up the gem paperclip to add attachments to a model. Almost everything works fine, like attaching images/jpg or even pdf.
But I can't upload zip files. I tried different zip files, but I'm always getting:
"Attachment Paperclip::Errors::NotIdentifiedByImageMagickError"
This is my model:
class Order < ActiveRecord::Base
has_attached_file :attachment, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :attachment, :content_type => ["application/pdf", "application/zip", "application/x-zip", "application/x-zip-compressed","application/octet-stream","image/jpg","image/png"]
belongs_to :client
has_one :status
end
I'm developing on a Mac (Yosemite), installed imagemagick via brew and using SQLite.
I added this to my Gemfile:
gem "paperclip", "~> 4.2"
gem 'cocaine', '~> 0.5.4'
I did research on google the last hours, and there are many people struggling with paperclip, but I didn't find anybody with problems uploading zip files.
Maybe someone can help here.
Thanks
ImageMagick can't read .zip files. See the acceptable file types here:
http://www.imagemagick.org/script/formats.php
If you're trying to generate a thumbnail from a zip file ImageMagick should fail every time.
Try adding this to your model:
before_post_process :skip_for_zip
def skip_for_zip
! %w(application/zip application/x-zip).include?(asset_content_type)
end
Then your app won't try to process zip files as images

Paperclip with sidekiq

I am using paperclip for uploading files. Does anybody have use it with sidekiq for background jobs?
I was trying to achieve something similar to railscast 383 (uploading to Amazon S3) but with paperclip and sidekiq.
I didn't find too much information for using it with sidekiq and I am thinking if I should change to carrierwave or if there is any example for paperclip and sidekiq (not with FancyUploader and delayed_jobs).
I think what you're looking for is the delayed_paperclip gem:
https://github.com/jrgifford/delayed_paperclip/
According to the gem it has a nice ActiveRecord api so you can do something like:
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
process_in_background :avatar
end

Paperclip is throwing a strange error all of a sudden after update

I can no longer render pages with paperclip urls in my Rails application. I recently updated my gem bundle. I'm using Rails 3.2.8 and Paperclip 3.1.4. I was using Paperclip 2.7.0 before.
The link in my view that is failing is:
ad.image.url(:medium)
My Ad model has these declarations:
Paperclip.interpolates :ad_subdomain do |attachment, style|
attachment.instance.brand.subdomain
end
has_attached_file :image,
:default_url => '/images/blank.gif',
:styles => { :medium => ["290x230>","jpg"],
:thumb => ["100x100>","jpg"] },
:storage => :file,
:path => "/mcp/ads/:style/:ad_subdomain/:basename.:extension"
The error that is being thrown is:
TypeError: wrong argument type Class (expected Module)
from /Users/me/.rvm/gems/ruby-1.9.3-p0#mcp5/gems/paperclip-3.1.4/lib/paperclip/attachment.rb:368:in `extend'
from /Users/me/.rvm/gems/ruby-1.9.3-p0#mcp5/gems/paperclip-3.1.4/lib/paperclip/attachment.rb:368:in `initialize_storage'
from /Users/me/.rvm/gems/ruby-1.9.3-p0#mcp5/gems/paperclip-3.1.4/lib/paperclip/attachment.rb:80:in `initialize'
from /Users/me/.rvm/gems/ruby-1.9.3-p0#mcp5/gems/paperclip-3.1.4/lib/paperclip/instance_methods.rb:5:in `new'
from /Users/me/.rvm/gems/ruby-1.9.3-p0#mcp5/gems/paperclip-3.1.4/lib/paperclip/instance_methods.rb:5:in `attachment_for'
from /Users/me/.rvm/gems/ruby-1.9.3-p0#mcp5/gems/paperclip-3.1.4/lib/paperclip.rb:191:in `block in has_attached_file'
For some reason, I had:
:storage => :file
in my declaration. That does not work. If you want to use the filesystem, you just leave out the storage line altogether and it will default.
Check out the requirements listed on the Paperclip github page:
Paperclip now requires Ruby version >= 1.9.2 and Rails version >= 3.0
(Only if you're going to use Paperclip with Ruby on Rails.)
If you're still on Ruby 1.8.7 or Ruby on Rails 2.3.x, you can still
use Paperclip 2.7.x with your project. Also, everything in this README
might not apply to your version of Paperclip, and you should read the
README for version 2.7 instead.

Resources