Reputation: 2085
I am trying to find documentation on how to associate images uploaded through cloudinary to a particular user that will allow deletion etc by the user that uploads them, but not by other users. However the images should be available to view publicly at all times. I'm relatively new to rails however essentially I am trying to replicate using cloudinary:
class Image
belongs_to :user
end
class User
has_many :images
end
Obviously the code is not complete just trying to show the association I'm trying to achieve, if this is a suitable way of representing the relationship. If anyone has suggestions for a better solution or can point me in the direction of some documentation that deals with an issue like this it would be very much appreciated!
Upvotes: 1
Views: 845
Reputation: 541
Assuming you use CarrierWave with Cloudinary for managing uploads of your Rails model. You need to define an uploader class and mount it to your Image class. Assuming your Image model class has a 'picture' attribute:
class PictureUploader < CarrierWave::Uploader::Base
include Cloudinary::CarrierWave
version :thumbnail do
process :resize_to_fit => [50, 50]
end
end
class Image
belongs_to :user
mount_uploader :picture, PictureUploader
end
Each user has multiple Image records, each holding a reference to an actual image being uploaded. For embedding such an image:
<%= image_tag(user.images[0].picture_url) %>
Or a thumbnail:
<%= image_tag(user.images[0].picture_url(:thumbnail)) %>
See Cloudinary documentation for more details.
Regarding image deletion: your web application probably allows users to manage images. Simply deleting an Image record would automatically delete the remote image uploaded to Cloudinary. For example:
user.images.last.destroy
Upvotes: 4