Paperclip path/url using values of an object - ruby-on-rails

I've been playing around with using Paperclip to build a photo gallery/store. A Gallery has many Photos, and a Photo belongs to a Gallery, and Users can have many Galleries. The paperclip defaults do something like /:class/:style/:basename.:extension. However, with a gallery setup, I'd much rather have something like /:class/:user_name/:gallery_name/:styles/:basename.:extension. I haven't yet found a way to access variables in an object in order to dynamically create these storage locations.
Is there any way of doing this?
I've tried using #{variable} in the path, but that doesn't work. These photo objects are being created using #gallery.photos.build, so the gallery_id should already have a value that's accessible.

Take a look at the tips and updates section on Thoughtbot.com. It discusses how to add your own interpolated variables into the path/url.

#zetetic's answer is a bit dated (the blog post is from 2008) The current (2015) way to create custom interpolations is described in the paperclip wiki. So for the user_name in the question, probably something like this:
# interpolate in paperclip
Paperclip.interpolates :user_name do |attachment, style|
attachment.instance.gallery.user.name
end

Related

Active Storage - Adding File Description / Text - Ruby on Rails 5.2

With the release of Rails 5.2, the much used Paperclip gem is now deprecated and it's advised to use Active Storage that ships with Rails. I'm starting a new project and set up Active Storage with ease, but my problem comes when trying to add a name or description to the file uploads.
With Paperclip I would add a column to the model called something like file_upload_name, so that as well as having a file name "something.pdf" I could also add a name or description such as "My Important Document" on the upload form.
For the projects that I'm doing, this is a vital part of the upload process and ideally needs to be done at the time of upload. As Active Record doesn't store to a model in such a way it's not as simple as just adding a column and adding fields to a form. It seems something that should be relatively simple but I can't figure it out or find any information about how best to do it. Any help much appreciated.
Here's an example of what I'm trying to achieve:
With Active Storage the end result is a multiple file upload button, with no naming etc.
You should create a new model to wrap each attached file. That model would then have the ActiveStorage attachment defined on it, as well as whatever other attributes you need to capture. Ex:
class Attachment < ApplicationRecord
has_one_attached :file
end
Rails then treats file kind of like an attribute for each Attachment. You can define your other attributes (e.g. upload_name, etc.) on the Attachment model. Based on your screenshot, it looks like maybe a Quotation has many attached files, so you'd do something like:
class Quotation < ApplicationRecord
has_many :attachments
end

How to append an image to a rails object dynamically

How can I append an image to a model object in rails? For example, if I had a post, how could I upload/append an image to the object so that I can call it in the view dynamically? I know that the association would be a has_one relationship... Ive looked around and just can't find anything on this topic. Thanks.
The most common solution for model attachments in Rails is the Paperclip gem. Try using it, or take a look at a corresponding Ruby Toolbox category.

Rails ActiveRecord associations with bulk image uploads

I have a model Item that has an attribute :code.
Items are added to the database via CSV file uploads in rails. Each :item should have a product image associated with it.
The research I've done so far seems to suggest that bulk image uploads (think 500-1000 images) are best handled outside of rails.
My question is this: if I upload bulk images to S3, is there any way to associate images to their respective :item? For simplicity, let's assume that we can easily infer :code from each images filename.
The end goal is to display an items image with something like:
<%= image_tag("#{#item.image}") %>
Let me know if I can clarify, thank you!
You gave a little to few information (e.g. what gem do you use to store the images) for an optimal answer. So here is the answer for the given question: Add the following method into your Item model:
def image
"example_image_#{self.code}.jpg"
end
Please be aware that image_tag() will always result into an asset pipeline path. Please see http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html#method-i-image_tag

Rails: Paperclip question regarding column names

I have two scenarios for using Paperclip, but I'm unsure of how to tweak the settings or if it's possible or even necessary. Need the advice of more seasoned professionals on this one.
First up, I have a Document model for uploads such as PDFs, which would be defined:
has_attached_file :document...
This would give me column names like #document.document_file_name. Anyway that I could have #document.file_name instead?
Secondly, I have Gallery.rb which has many Picture.rb. Same scenario here as well. Can I avoid having #picture.picture_file_name? Or is this something that should really be overlooked with the gains that Paperclip affords.
Thanks in advance for any input.
My take on this: The actual document (PDF file) is not the same as a document record (which comprises the physical document plus metadata). Therefore it makes sense to see the Paperclip attachment as an attribute of the model and have its methods be called after the attribute, and not operate on the model record itself.
One of my apps has a Document model with an attached file too, and I simply called the attribute attachment.
If this is too much of an inconvenience for you, you could always implement your own getters in the model:
class Document < ActiveRecord::Base
has_attached_file :attachment # ... or whatever you are calling it
def file_name
self.attachment.file_name
end
def file_size
self.attachment.file_size
end
def file_type
self.attachment.file_type
end
end
The Paperclip gem requires three attributes on the associated object.
attribute_file_name
attribute_file_size
attribute_file_type
attribute of course if the name of your file and it is the has_attached_file :attribute filed commonly called picture, image, whatever.
If you want to change one of those names you will need to edit the gem itself which seems crazy for just changing the attribute name :)
Here are the methods I had to create:
{attribute}_file_name
{attribute}_file_size
{attribute}_content_type
{attribute}_updated_at

Rails: Image Upload with Tableless Model

Here is the scenario, i would like the user to input all the data and all and use em to populate a result. I won't need to store them in a database since i will just be showing them a result page.
I did http://railscasts.com/episodes/219-active-model and made my model tableless.
But now i have a problem when i wanna receive image upload from the user. I would also like to display that picture in the result page, and since i will just be using it once, if possible i wouldnt wanna store it in the database as well.
I tried implementing paperclip with the tableless model (since i couldnt find any other solution) but it seems that the model has inherit ActiveRecord::Base for it to work...
Is this possible? Or is this other way i can implement this?
Thanks!
If you were to succeed with using Paperclip for this, how would you get rid of the uploaded image once you no longer needed it? Without a database or some other form of persistent storage, how would you know where the image had been stored?
I think that you have some conceptual issues here that you should rethink before you start hacking up tableless models that accept image uploads.
But, if for some reason you really want to do it this way, then I would suggest just uploading the image without the benefit of a gem like Paperclip, which is really intended to make it easier to associate files with ActiveRecord objects. Just google for how you upload a file in Ruby, it's not really all that difficult.
OK, so you want to receive an image, and then display it right back, and not store the image. Can do.
What about a Controller that receives a file using multipart, and then transmits the file back to the request?
Controller file:
def upload
# Save file
name = params['datafile'].original_filename
directory = "tmp/uploads"
temp_file_name = File.join(directory, name)
send_file temp_file_name, :status=>200
end
You'll then just cleanup tmp when you need. Or, try out doing a File.delete temp_file_name when you need to.
If you want to validate that it's an image, you can do Paperclip model validation.

Resources