回形针图像是否不会与伪造的宝石数据一起上传到Ruby on Rails中?

时间:2018-12-28 11:06:48

标签: ruby-on-rails ruby paperclip faker

我正在尝试使用伪造的gem上传伪造的图像数据,但是我看到此错误:

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

在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

在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

在db / add_attachment_item_image_to_images.rb(回形针gem迁移文件)中

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

4 个答案:

答案 0 :(得分:0)

尝试一下,

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

您的图片必须位于app/assets/images

或尝试关注

File.open(File.join(Rails.root, "app/assets/images/image.jpg"))

答案 1 :(得分:0)

如果下一个选择是FactoryGirl,则可以在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

答案 2 :(得分:0)

Faker::Avatar.image('app/assets/image.jpg')返回带有图像占位符(String)的URL的"https://robohash.org/app/assets/image.jpg.png?size=300x300&set=set1",但是Paperclip希望在那里有文件或IO。

因此,您应该传递File.new(File.join(Rails.root, 'spec', 'fixtures', 'files', 'avatar.jpg'))之类的东西(并在该路径上放置一些图像)

答案 3 :(得分:0)

有效

 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