2017-07-31 85 views
1

我在軌道上使用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/[email protected]/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:268:in `load' 
/home/aspireedge/.rvm/gems/[email protected]/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:268:in `block in load' 
/home/aspireedge/.rvm/gems/[email protected]/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:240:in `load_dependency' 
/home/aspireedge/.rvm/gems/[email protected]/gems/activesupport-4.2.9/lib/active_support/dependencies.rb:268:in `load' 
/home/aspireedge/.rvm/gems/[email protected]/gems/railties-4.2.9/lib/rails/engine.rb:547:in `load_seed' 
/home/aspireedge/.rvm/gems/[email protected]/gems/activerecord-4.2.9/lib/active_record/tasks/database_tasks.rb:253:in `load_seed' 
/home/aspireedge/.rvm/gems/[email protected]/gems/activerecord-4.2.9/lib/active_record/railties/databases.rake:173:in `block (2 levels) in <top (required)>' 
/home/aspireedge/.rvm/gems/[email protected]/gems/rake-12.0.0/exe/rake:27:in `<top (required)>' 
/home/aspireedge/.rvm/gems/[email protected]/bin/ruby_executable_hooks:15:in `eval' 
/home/aspireedge/.rvm/gems/[email protected]/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 

回答

-1

我解決這個錯誤

  1. 創建在迪爾一個圖像文件夾。
  2. 你在指定的圖像root_path到種子文件

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

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