Paperclip: How to store a picture in a Rails console?

邮差的信 提交于 2019-12-31 08:29:12

问题


I tried storing a local image in a rails console.

Because I have many pictures in my local storage (I use crawler to download tons of pictures), I want to store them into a database, with the benefit of paperclip to do some image job, like thumbnail etc. If I use a webpage to save new pictures to database one by one, it will cost a lot of time. So I want to find a way in rails console (some code) that can batch save-picture-into-database.


回答1:


To further clarify @andrea's answer:

YourPaperclippedModelHere.new(:your_paperclip_field => File.new(path, "r"))

So if your model is called Image and your paperclip field is data:

Image.new(:data => File.new(path_to_your_file, "r"))




回答2:


If this is the model:

class User < ActiveRecord::Base
  has_attached_file :avatar
end

then the following should work from the console:

>> User.create(:avatar => File.open('/path/to/image.jpg', 'rb'))



回答3:


I dont know if it is what you want ... but to save an paperclip asset from console You could simple use a File instance . a.e.

Image.new :data=>File.new("/path/to/image.jpg","r")



回答4:


Late Answer but hopefully it will work for others. You need to include.

File.new("#{Rails.root}/public/images/default_avatar.png", "r")



来源:https://stackoverflow.com/questions/4680265/paperclip-how-to-store-a-picture-in-a-rails-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!