Rails Paperclip Gem - save parent model ID to path - ruby-on-rails

I have a model of ThreesixtyViewer which also has a nested resource of the ThreesixtyViewerImage model. An image attribute is being saved via the paperclip gem - but I am having issues updating the file path how it's needed.
The images for each ThreesixtyViewer needs to be saved together within one directory that is associated with the specific viewer. For example:
/public/system/threesixty_viewer_images/12/large/filename.jpg
In this example, the 12 in the path would be the id of the specific threesixtyviewer - but I cannot find any examples with that functionality. If the ThreesixtyViewer had an ID of 57, then the path would look like so:
/public/system/threesixty_viewer_images/57/large/filename.jpg
threesixty_viewer.rb
belongs_to :article
has_many :threesixty_viewer_images, dependent: :delete_all
accepts_nested_attributes_for :threesixty_viewer_images, allow_destroy: true
threesixty_viewer_image.rb
belongs_to :threesixty_viewer
has_attached_file :image, styles: { small: "500x500#", large: "1440x800>" },
path: ':rails_root/public/system/:class/:VIEWER_ID/:size/:filename',
url: '/system/:class/:VIEWER_ID/:size/:filename'
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
I know the :path and :url attributes need to beupdated for the has_attached_file within the threesixty_viewer_image.rb - but I am unsure as to how I can get the id for the threesixty_viewer... for now I added a :VIEWER_ID in it's place.
Any help would be greatly appreciated! Thanks in advance for anyone who can lend an eye!

You can add any of the models attributes to that path for this object. I believe you can even go so far as adding anything that the method will respond to, so you can even create path helpers for returning specific strings (such as month it was saved, year, etc).
In your case, ThreesixtyViewerImage is a child model, your table should include a column for the parent model. In your case, that attribute is probably :threesixty_viewer_id
Here is what I think you need for setting that path on threesixty_viewer_image.rb:
has_attached_file :image,
styles: { small: "500x500#", large: "1440x800>" },
path: ":rails_root/public/system/:class/:threesixty_viwer_id/:size/:filename",
url: "/system/:class/:threesixty_viewer_id/:size/:filename"
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
EDIT
Everything I said above is dead wrong. My apologies! What you need to use is a Paperclip::Interpolation. Here's a link
Here's what I did to make use:
Create a new file: config/initializers/paperclip_interpolators
Place something like this in that file:
Paperclip.interpolates :threesixty_viewer_id do |attachment, style|
attachment.instance.threesixty_viewer_id
end
Restart your application
Regenerate your paths/folders for the attachments. Here's a Link
Anytime you want another attribute in your path, just add another interpolator! Sorry again for misleading you before.

#colin_hagan is on the right path - I would suggest you look into Paperclip Interpolations:
#app/models/threesixty_viewer_image.rb
class ThreesixtyViewerImage < ActiveRecord::Base
belongs_to :threesixty_viewer
has_attached_file :image,
path: ":rails_root/public/system/:class/:threesixty_viewer_id/:size/:filename",
url: "/system/:class/:threesixty_viewer_id/:size/:filename"
Paperclip.interpolates :threesixty_viewer_id do |attachment, style|
attachment.instance.threesixty_viewer_id
end
end
Notice the double quotes, rather than single.
Double quotes should be used for interpolation - single quotes are for literal strings (I think).
Another thing I found some time back is the paperclip_defaults option -- allowing you to specify styles etc for any attachments:
#config/application.rb
...
config.paperclip_defaults = {
styles: { small: "500x500#", large: "1440x800>" }
}
These can be overridden in your respective models - it's just handy for me, as it means you don't have to explicitly define styles each time you have an attachment.

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.

Rails Paperclip copy image with hash from one model to another

I want migrate my images from one model to another.
My old model looks like:
class Post < ActiveRecord::Base
has_attached_file :logo,
url: '/test/post/logo',
path: ':rails_root/uploads/test/post/:id/logo/:hash',
hash_secret: 'secret',
styles: { thumb: ['200x150>', :jpg], medium: ['320x240>', :jpg], large: ['480x360>', :jpg] }
end
the new model looks like the old one.
Currently i try to migrate the images with the following code snippet:
new_logo = post.logo
new_image.logo = new_logo
new_image.save
But unfortunately is doesn't work. I get the following error:
No such file or directory # rb_sysopen -
/abcde/fghjk/test/post/1/logo/43023e427c1deb69789bbf7b75cf32810fbb6354
When i search for the hash in the directory, it doesn't match some of the hashes.
Without hashing it will work like a charme but i need a solution with hashed attachments.
Have someone an idea to solve my problem?
You can read the file using .path and then assign the file object:
new_image.logo = File.open(post.logo.path)
new_image.save

paperclip - path in image.rb

I am using paperclip for uploading images in my rails application.
I have 2 models 'Image' and 'Book' where one book can have many Images and image belongs_to Book.
In my 'images' table, I have 4 columns:
id
book_id
data_file_path
data_file_name
column 'data_file_path' stores the path of the image and column 'data_file_name' stores the image name and extension
In my image.rb file, I have the following lines of code:
has_attached_file :data, :path => "http://www.test.org/book/book_images/:data_file_path/:book_id/:style/:basename.:extension",
:url => "http://www.test.org/book/book_images/:data_file_path/:book_id/:style/:basename.:extension"
Notice the ':data_file_path' in the code above.
In my view, when i do <%=image.data.url(:thumb)%> i get the following path:
http://www.test.org/book/book_images/data_file_path/9124/thumb/1897_EK91_201107.jpg
Instead of retrieving the data_file_path value from the d-base, it's displaying the variable name.
How to I get the data_file_path value in the url.
Thanks a lot for your precious help. I have struggling with that since a while now.
Ah, I finally figured it out. I needed to use Paperclip.interpolates.
I added the following in config/initializers/paperclip.rb file
Paperclip.interpolates :data_file_path do |attachment, style|
attachment.instance.data_file_path
end
Then just add 'data_file_path' to the path in image.rb:
has_attached_file :data, :path => "http://www.test.org/book/book_images/:data_file_path/:book_id/:style/:basename.:extension",
:url => "http://www.test.org/book/book_images/:data_file_path/:book_id/:style/:basename.:extension"
Hope this might be of help to somebody else :)
cheers

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.

Resources