Save CarrierWave Upload as Base64 - ruby-on-rails

I would like to take an uploaded attachment from CarrierWave and save it as a Base64 string in my database table, as opposed to saving the path of the upload.
I have absolutely no idea where to start the process. I tried to create the following:
........
before_create :process_image
mount_uploader :image_upload, ImageUploader
.......
attr_accessible :name, :image_upload, :authentication_type
attr_reader :image
private
def process_image
if !self.image_upload.path.nil?
self.image = Base64.encode64(file.open(self.image_upload.path).read)
self.image_upload = nil
end
end
However, during the upload I get the following error:
NoMethodError (undefined method `image_upload_will_change!'
I am sure it's something trivial, however, it seems like I am the only one that wants to try this.

Related

Carrierwave object.url VS object.image_url

In my Rails 5 app I am using Carrierwave to upload images.
I have to model that uses the same uploader:
account.rb:
mount_uploader :logo, ImageUploader
image.rb:
mount_uploader :image, ImageUploader
This uploads the file to:
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
The strange this now is that I can use:
#account.logo&.url(:thumb) // works!
#account.logo&.image_url(:thumb) // error!
But on the image model (one product has many images):
#product.images.first&.image_url(:thumb) // works!
#product.images.first&.url(:thumb) // error!
So in the first case I have to use .url and in the second one .image_url
An I have no idea why...any help?
The instance method image_url is defined dynamically based on the column that is passed to mount_uploader and simply calls url on the column. The definition looks like this...
def #{column}_url(*args)
#{column}.url(*args)
end
So, I would suspect that logo_url would work on #account (although I have not tested this)
source

Carrierwave remote attachment url not working on creation

I have the following model called ApplicationAttachment in my Ruby on Rails project.
I also have uploaded files on my s3 bucket. When i try to upload the image to my model, i dont get an error but attachment is nil and the remote_attachment_url is not saving the file. Not sure what is the problem.
My code is this
ApplicationAttachment.create!(remote_attachment_url: "http://www.jqueryscript.net/images/jQuery-Plugin-For-Fullscreen-Image-Viewer-Chroma-Gallery.jpg")
This doesnt return any error but it doesnt save the image.
class ApplicationAttachment < ActiveRecord::Base
mount_uploader :attachment, DeveloperReferralAttachmentUploader
attr_accessible :id, :attachment, :remote_attachment_url, :created_at, :updated_at
end
class DeveloperReferralAttachmentUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def extension_white_list
%w(pdf jpg jpeg gif png)
end
end
How do i ensure that the remote_image_url saves when created via Carrierwave.
Thanks
I think you might want to explicitly save the attachment instead of using create.
aa = ApplicationAttachment.new(
remote_attachment_url: "http://www.jqueryscript.net/images/jQuery-Plugin-For-Fullscreen-Image-Viewer-Chroma-Gallery.jpg"
)
aa.save

upload image to multiple models in rails

I am using carrierwave and Minimagick gem to upload an attachment to S3. Now I want to save the some.pdf in two models(ie, assignment, and message). I give the same parameters in attachment field to save in two tables. But the second table attachment saves blurry. First one gets clear view of attachment.
My controller codes like,
#assignment = Assignment.new(assignment_params)
#message = Message.new
begin
Message.transaction do
asign_att = params[:assignment][:attachment]
#assignment.save!
#message.attachment = asign_att
#message.save!
end
end
My model has,
(in attachment.rb) mount_uploader :attachment, AttachmentUploader
(in message.rb) mount_uploader :attachment, ImageUploader
I want to save same file into two models with clear view. What I want to do? Thanks in advance.
Check in your second table uploader file if you have specified any version or something like that.
With version you can create clones of attachment in different resolutions like this.
version :thumb do
process resize_to_fit 50, 50
end
I would use a callback to do this, something like:
after_commit :assign_to_models
def assign_to_models
...
end
IMHO, I would create an model that has all the carrierwave attachements, and have it belongs both to message and attachement.
I hope this helps

Carrierwave: How to validate that image is attached? (either upload, or remote_url)

I'm trying to make sure that every instance of Picture model has a file attached.
In other words - there are two fields in the form:
:file
:remote_file_url
I want user to fill in at least one of them.
When I validate presence of :file, and submit remote_file_url instead of file - then it gives validation error.
The only way I found for now is do smth like this:
class Picture < ActiveRecord::Base
validate :file_xor_remote_file_url
private
def file_xor_remote_file_url
if !(file.blank? ^ remote_file_url.blank?)
errors.add(:base, "Specify a file to upload, or file URL, not both")
end
end
end
The wiki says you can validate your upload like this:
mount_uploader :avatar, AvatarUploader
validates_presence_of :avatar
It doesn't mention any different handling for remote URLs, but underlines that Carrierwave validates the presence of the file rather than just the presence of an URL. So a given remote URL must refer to a valid file that can be uploaded.
You said that "validate presence of :file", but perhaps this could be the point. Hope this helps.

Rails 3 Dragonfly uid of attachment is correct but the file iself is nil

Im using Dragonfly and Amazon s3 for the image uploads. For some reason when I upload a picture, it saves to the right folder on amazon, and the uid is the right path, but it is not showing up!
Every time I call user.avatar it is nil even though user.avatar_uid is correct. How can I get the image to display properly with user.avatar.remote_url?
class User < ActiveRecord::Base
image_accessor :avatar do
storage_path { |file|
"#{self[:id]}/avatar/pic#{rand(1000)}.#{file.format.to_s.downcase}"
}
after_assign { |a|
self.avatar = a.jpg.thumb('300x300#n') if (VALID_PHOTO_TYPES.include? self.avatar.format)
}
end
attr_accessible :avatar_url, :retained_avatar, :avatar
attr_reader :id, :avatar_uid
The problem is the :avatar_uid in any of attr_reader, attr_writer, attr_accessible.
If you have that in your model, it will break. Pretend that the *_uid does not exist for any model with Dragonfly and only use user.avatar.

Resources