Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4619

How to save image with Paperclip for specific model record

$
0
0

I have a model where each record has an image with a :background field that is used on the frontend. Paperclip is already used in the project, and in the admin panel, I can upload this image, so that capability exists. However, I currently do not have access to the admin panel code. Also, I cannot edit an existing record.

I searched for solutions and found the following instructions:

  1. I uploaded the image to the server.
  2. I located the desired record by model.
  3. I tried to save it using the following line of code:
new_pic = File.new('/path/to/picture/picture.png')record.background = new_pic

I also tried the following code:

Model.new(background: File.new('/path/to/picture/picture.png', 'r'))

However, both resulted in the following error:

Paperclip::Errors::MissingRequiredValidatorError: Paperclip::Errors::MissingRequiredValidatorError

I looked at the validations, but there are none related to Paperclip. There are only similar ones related to the field where Paperclip is used:

has_attached_file :background,  path: ':rails_root/public/some/path/:id/:style/:basename.:extension',  url: '/some/path/models_name/:attachment/:id/:style/:basename.:extension'

How can I properly attach a file to a record?


Viewing all articles
Browse latest Browse all 4619

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>