No thumb folder generate by paperclip when uploading from url - ruby-on-rails

I have the following ToyPhoto model:
require "open-uri"
class ToyPhoto < ActiveRecord::Base
belongs_to :toy
has_attached_file :image, :styles => {
:thumb => ["210x210>", :jpg]
}
def image_url=(value)
self.image = open(value)
end
end
When I upload a photo, I don't see the corresponding thumb folder being created. This is how I create the ToyPhoto objects:
params[:photos].each do |photo|
#toy_photo = ToyPhoto.new
#toy_photo.image_url = photo[:url]
#toy_photo.save
#toy.photos << #toy_photo
end
I do see photo being successfully uploaded to original folder, but no thumb folder created. Did I miss some configuration problem? I am suspecting that strong_parameter didn't defined when uploading from url, however I am not sure how to set strong_parameter in this case.
Thanks!

I believe that the problem in your code is how you specify the format of the thumb style. Reading the Paperclip API showed that the proper way to do this is like so:
has_attached_file :image, :styles => { :thumb => ["210x210#", :jpg] }

Related

Uploaded images to cloudinary saves the images three times in my cloud storage

Please i am facing this problem with my configuration on uploading images to my cloud storage(Cloudinary).
when i upload a single image in my rails app, it makes a duplicated of times 3 in my cloud storage.
I am Using ruby 2.3 & Rails 5.1 with paperclip-cloudinary ~> 1.3', '>= 1.3.2
I am pretty sure is the path that i set.
i dont know how to set the right path.
:path => ':class/:id/:style/:filename'
My Post Model
class Post < ApplicationRecord
acts_as_votable
belongs_to :user
has_many :comments
has_attached_file :image,
:storage => :cloudinary,
:path => ':class/:id/:style/:filename',
styles: { medium: "700x500#", small: "350x250>" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end
the problem was the :style in the path
:path => ':class/:id/:style/:filename'
to prevent making duplicate of the styles defined, remove the style option from the path
for it to be like this =>
:path => ':class/:id/:filename'
It's not a duplicate image. It's a three images of different dimensions that you are mentioning in styles.
1st one is your original image that you are actually uploading.
Second and third will be of medium: "700x500#", small: "350x250 respectively.
You can call other two's are thumbnails of the original image.

Update a file without uploading it, using paperclip.

I use paperclip on a logo model and have the following setup.
class Logo < ActiveRecord::Base
has_attached_file :attached,
:styles => {
:small => ["170x170>", :jpg], :large => ["400x400>", :jpg]
}
end
If I update the picture of logo, the file should be uploaded by paperclip.
However, I do not want it to upload, I only wish to update the logo's attributes in the database.
Do you have any good ideas?
What about simply doing logo.some_attribute = "value" then logo.save ?

Custom Paperclip processor not being called

I have a user model that is generated using Devise. I am extending this model using paperclip to enable file upload and also the processing of a file using a custom paperclip processor.
My paperclip field is declared in the user model as follows. PaperClipStorage is a hash that I create with the paperclip variables. Also, the being stored on AWS S3.
has_attached_file :rb_resume, PaperclipStorageHash.merge(:style => { :contents => 'resume_contents'}, :processors => [:resume_builder])
validates_attachment_content_type :rb_resume, :if => lambda { |x| x.rb_resume? }, :content_type => ['application/pdf', 'application/x-pdf', 'application/msword', 'application/x-doc']
The validates_attachment_content_type check is being done to make sure that it only processes pdf and MS word files.
My processor looks as follows
module Paperclip
class ResumeBuilder < Processor
def initialize(file,options = {}, attachment = nil)
#file = file
#attachment = attachment
puts "Attachment is not null " if !attachment.nil?
end
def make
rb = MyModule::MyClass.new(#file.path) ### Do something with the file
section_layout = rb.parse_html
#attachment.instance_write(:whiny, section_layout)
#file
end
end
end
In my user model I also have an after_save callback that is supposed to take the section_layout generated in the processors make method. Code is as follows
after_save :save_sections
def save_sections
section_layout = rb_resume.instance_read(:whiny)
# Do something with section_layout...
end
Now my problem is that the processor code is never being called, and I can't figure out why.
Because of that the section_layout variable is always nil.
Another point to note is that the same model also has two other has_attached_file attributes. None of the other two use a custom processor.
I've been struggling with this for last 3 hours. Any help would be greatly appreciate.
Thanks
Paul
Error in my has_attached_file declaration
has_attached_file :rb_resume, PaperclipStorageHash.merge(:style => { :contents => 'resume_contents'}, :processors => [:resume_builder])
should actually be
has_attached_file :rb_resume, PaperclipStorageHash.merge(:styles => { :contents => 'resume_contents'}, :processors => [:resume_builder])
Notice the plural styles as opposed to singular style

How to set Paperclip :default_url according User gender and asset style?

Through this gist script I can get default url for paperclip with user gender. But I want to get :style too. Someone know how to?
Default way of doing this:
#Assuming you default images are stored in app/assets/images/somedirectory
has_attached_file :attachment, default_url: :some_function
def some_function
"somedirectory/:style/:class/#{self.gender}.png"
end
or using lambda:
has_attached_file :attachment, default_url: lambda{|attachment| "somedirectory/:style/:class/#{attachment.instance.gender}.png" }
Tested in Rails 4.

Paperclip: Is that possible to recreate a deleted thumbnail without uploading the original image again?

I use Paperclip like this:
class Asset < ActiveRecord::Base
has_attached_file :asset, :styles => { :thumb => "80x80>" }, ...
When an image is uploaded, a thumbnail is created successfully.
Suppose, that for some reason, a thumbnail was deleted from the file system.
Is that possible to recreate the thumbnail without uploading the original image again ?
I tried:
Asset.find(16).save # => true
but the thumbnail wasn't created in the file system.
Have you tried the reprocess! method? Something like
Asset.find(16).asset.reprocess!

Resources