-3

I'm trying to upload fake image data using faker gem, but I am seeing this error:

Paperclip::AdapterRegistry::NoHandlerError: No handler found for "https://robohash.org/app/assets/image.jpg.png?size=300x300&set=set1"

in db/seed.rb

5.times do 
    Image.create([{
        filename:Faker::Food.dish,
        title:Faker::Food.dish,
        item_image:Faker::Avatar.image('app/assets/image.jpg')
                }])
        end

in db/image.rb

    class CreateImages < ActiveRecord::Migration[5.2]
      def change
        create_table :images do |t|
          t.string :title
          t.string :filename
          t.timestamps

        end
      end

    end

in db/add_attachment_item_image_to_images.rb (paperclip gem migration file)

class AddAttachmentItemImageToImages < ActiveRecord::Migration[5.2]
  def self.up
    change_table :images do |t|
      t.attachment :item_image
    end
  end

  def self.down
    remove_attachment :images, :item_image
  end
end
rlandster
  • 7,294
  • 14
  • 58
  • 96
adarsh
  • 306
  • 5
  • 16

4 Answers4

0

Try with,

Faker::Avatar.image('image.jpg')

Your image must be in app/assets/images

or try following,

File.open(File.join(Rails.root, "app/assets/images/image.jpg"))
ray
  • 5,454
  • 1
  • 18
  • 40
  • 20.times do Image.create( item_image: Rails.root.join("app/assets/images/food_image1.jpg").open, filename: "food", title: "food" ) end it tryed this way it work,but it store only one img all time. – adarsh Dec 28 '18 at 12:01
  • If you have 20 images then you can do like, `20.times do |n|; Image.create( item_image: Rails.root.join("app/assets/images/food_image#{n}.jpg").open, filename: "food", title: "food" )` – ray Dec 28 '18 at 12:04
0

if FactoryGirl is your next option, you can use as follows in spec/factories.rb:

include ActionDispatch::TestProcess
FactoryGirl.define do
    factory :picture do 
      original_filename "test.jpg"
      file { fixture_file_upload(Rails.root.to_s + '/spec/fixtures/files/test.jpg', 'img/jpeg') }
  end
end
Dev.rb
  • 487
  • 6
  • 14
0

Faker::Avatar.image('app/assets/image.jpg') returns a String with url to image placeholder ("https://robohash.org/app/assets/image.jpg.png?size=300x300&set=set1"), but Paperclip expects a file or IO there.

So you should pass something like File.new(File.join(Rails.root, 'spec', 'fixtures', 'files', 'avatar.jpg')) (and place some image at that path)

Vasfed
  • 18,013
  • 10
  • 47
  • 53
0

it work

 20.times do
    Image.create(
     item_image: Rails.root.join("app/assets/images/image.jpg").open,
     filename: "Whole Wheat Pasta in Mushroom Sauce",
     title: "pasta"
                )
     end
adarsh
  • 306
  • 5
  • 16