使用回形针从种子文件上传图像 gem

Upload Image From seed file using paperclip gem

我在 rails 上使用 Ruby 并在旁边使用回形针进行图像存储和链接。它工作得很好。我想将图像添加到数据库中并抛出种子。 Rb 那么正确的语法是什么?

seed.rb

#add 2 Collection
Collection.create(title: 'Beauty, Health', Description: '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>')
Collection.create(title: 'Mobiles, Computers', Description: '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>')


#ADD 3 PRODUCT IN EACH COLLECTION 

Product.create(name:'Make-up', title: 'Make-up', description: '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>',
               price: '29', stars: '4', review: '<p>Love this.. always use Lakme compact for the quality purpose.. works well for me everytime..</p>',
               collection_id: '3', product_image: File.new("images/seed3.jpg"))

(/home/aspireedge/Aspiree/TestShop/app/assets/images) <== image_location

错误:

***@***-H81M-S:~/***/***$ rake db:seed
rake aborted!
Errno::ENOENT: No such file or directory @ rb_sysopen - images/seed3.jpg
/home/***/***/***/db/seeds.rb:18:in `initialize'
/home/***/***/***/db/seeds.rb:18:in `new'
/home/***/***/***/db/seeds.rb:18:in `<top (required)>'
/home/***/.rvm/gems/ruby-2.1.2@subscription/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:268:in `load'
/home/aspireedge/.rvm/gems/ruby-2.1.2@subscription/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:268:in `block in load'
/home/aspireedge/.rvm/gems/ruby-2.1.2@subscription/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:240:in `load_dependency'
/home/aspireedge/.rvm/gems/ruby-2.1.2@subscription/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:268:in `load'
/home/aspireedge/.rvm/gems/ruby-2.1.2@subscription/gems/railties-4.2.9/lib/rails/engine.rb:547:in `load_seed'
/home/aspireedge/.rvm/gems/ruby-2.1.2@subscription/gems/activerecord-4.2.9/lib/active_record/tasks/database_tasks.rb:253:in `load_seed'
/home/aspireedge/.rvm/gems/ruby-2.1.2@subscription/gems/activerecord-4.2.9/lib/active_record/railties/databases.rake:173:in `block (2 levels) in <top (required)>'
/home/aspireedge/.rvm/gems/ruby-2.1.2@subscription/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
/home/aspireedge/.rvm/gems/ruby-2.1.2@subscription/bin/ruby_executable_hooks:15:in `eval'
/home/aspireedge/.rvm/gems/ruby-2.1.2@subscription/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)

Product.rb(型号)

class Product < ActiveRecord::Base

  belongs_to :collection

  has_attached_file :product_image, styles: { product:"253x144", medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
  validates_attachment :product_image, content_type: { content_type: ["image/jpg", "image/jpeg",     "image/png"] } 
end

这应该有效:

 product = Product.new(name:'Make-up', title: 'Make-up', description: '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>',
               price: '29', stars: '4', review: '<p>Love this.. always use Lakme compact for the quality purpose.. works well for me everytime..</p>',
               collection_id: '3')
 product.product_image = File.open("images/seed3.jpg")
 product.save

我解决了错误

  1. 您在您的目录中创建了一个图像文件夹。
  2. 您将 root_path 图像分配给种子文件

product_image: File.new("#{Rails.root}/images/seed3.jpg"))