使用paperclip gem从种子文件上传图像

时间:2017-07-31 09:28:13

标签: ruby-on-rails ruby paperclip

我在轨道上使用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)&lt; == 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

2 个答案:

答案 0 :(得分:0)

这应该有效:

 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 :(得分:0)

我解决了错误

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

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