How to upload a local file to a Carrierwave model?

2020-06-01 08:59发布

问题:

I'm using Carrierwave to handle image uploads, but I'm not using a form, instead I use local files in the server.

How can I make this work?

@user = User.first
image_path = "/tmp/pic-s7b28.jpg"

@user.image = image_path
@user.save!

回答1:

@user = User.first
image_path = "/tmp/pic-s7b28.jpg"

@user.image = File.open(image_path)
@user.save!

You can check examples in the carrierwave readme