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
Related
This might be a stupid question since I can't find it already asked here but I can't find a solution no matter how I tried.
I use Rails 5 with Cloudinary and Attachinary gems.
I have a model PointOfInterest with
has_attachments :photos, maximum: 3
Everything works fine if photos are uploaded at once (with simple_form). But I want my users to be able to upload photos sequentially ; for instance 1 photo at the creation of the point_of_interest and then add 2 more later on edit.
Unfortunately the update method deletes the attachments before saving the new input.
I ended up building an array with the old attachment and new one from the update and tried to overwrite the attachments:
#point_of_interest.photos = new_photoset
but I get a Runtime error => Can't modify frozen hash.
I also just realized my new_photoset array mixes Attachinary::File (previous image) and Attachinary::File::ActiveRecord_Associations_CollectionProxy (new image) so I'm pretty sure I'm not on the right track.
I tries many different approaches and I don't find the Attachinary docs very helpful.
Or maybe I should investigate on the jQuery-File-Upload side?
Any help will be much appreciated.
I would suggest you split the actions in two steps. Add to your model:
has_attachment :point_of_interest_picture
and then step one would be to have the user upload a picture, and step two would be to upload two or more pictures
I have a LineItem class that has an images column. It belongs to an Order class. When a line_item is saved, the images column is a list of comma delimited image urls.
I want is to create a collage from the urls and save it as an attachment of the line_item instance.
I'm not sure where to start, though. Any idea what my work flow should look like?
Should I even use CarrierWave? Where should the image processing happen? In the LineItem model or the uploader file provided by CarrierWave?
I'm building an app for reviewing movies. I've cached a lot of movie data from an API. Essentially I have a movie model which has an :image attribute for Paperclip. What I've done is cache the remote image for each movie from the API onto my file system using a rake task. What I would like to do is the following:
For every movie I retrieve from the API with an image, set it's :image attribute to be the same image just stored on my file system for that particular movie.
Is something like this possible with Paperclip? Or do I need to look into something else?
With my current code, I can get reference to each image object but would Paperclip allow me to set the value of my model's :image attribute to the raw image object?
Yeah, shouldn't be a problem.
movie = Movie.first
movie.image = File.open("...")
movie.save!
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
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.