How Do I Use Factory Girl To Generate A Paperclip Attachment?

You can use fixture_file_upload include ActionDispatch::TestProcess in your test helper, here is an example factory: include ActionDispatch::TestProcess FactoryBot.define do factory :user do avatar { fixture_file_upload(Rails.root.join(‘spec’, ‘photos’, ‘test.png’), ‘image/png’) } end end In the above example, spec/photos/test.png needs to exist in your application’s root directory before running your tests. Note, that FactoryBot is a new name … Read more

Uploading multiple files with paperclip

Here is my code that worked well to upload multiple file using paperclip: We can achieve using nested attributes or using normal easy method. The following code shows normal method: User.rb has_many :images, :dependent => :destroy Image.rb has_attached_file :avatar, :styles => { :medium => “300×300>” } belongs_to :user users/views/new.html.erb <%= form_for @user, :html => { … Read more

Paperclip exception : Paperclip::AdapterRegistry::NoHandlerError

This Error is raised because you aren’t giving Paperclip a correct class. It’s a just a String. You should receive something like this in params “asset”=> {“image”=> #<ActionDispatch::Http::UploadedFile:0x000000056679e8 @content_type=”image/jpg”, @headers= “Content-Disposition: form-data; name=\”asset[image]\”; filename=\”2009-11-29-133527.jpg\”\r\nContent-Type: image/jpg\r\n”, @original_filename=””2009-11-29-133527.jpg””, @tempfile=#<File:/tmp/RackMultipart20120619-1043-yvc9ox>>} And you should have something like this in yout View (in HAML, very simplified): = form_for @product, html: … Read more

Save image from URL by paperclip

In Paperclip 3.1.4 it’s become even simpler. def picture_from_url(url) self.picture = URI.parse(url) end This is slightly better than open(url). Because with open(url) you’re going to get “stringio.txt” as the filename. With the above you’re going to get a proper name of the file based on the URL. i.e. self.picture = URI.parse(“http://something.com/blah/avatar.png”) self.picture_file_name # => “avatar.png” … Read more

tech