undefined method `version' for main:Object carrierwave - ruby-on-rails

I have carrierwave's uploader working without applying any changes to uploaded images. Though when I comment out the version block in the generated uploader file I get an "undefined method `version' for main:Object" message.
version is a built in method, and based on what googled, and even a railscast video on it I should be able to just uncomment it and roll.
# Create different versions of your uploaded files:
version :thumb do
process :resize_to_limit => [313, 344]
end
What could I have going on in my codebase that would be causing an issue with carrierwave's version method?

Error implicitly says that your version method not in scope of carrierwave uploader.
Your uploader should look like this.
class Uploader < CarrierWave::Uploader::Base
version 'anything' do
....
end
end
But as per error your uploader code is some thing like this:
class Uploader < CarrierWave::Uploader::Base
...
end
version 'anything' do
....
end
As your version method is not present inside uploader. Ruby considering it for main object when try to execute.
Few more thing also you need to make sure present in your code. You must have rmagick gem in your gemfile. You have imagemagick installed on your development machine.
include CarrierWave::RMagick uncommented in your uploader.

Related

How to edit existing image using Carrierwave and MiniMagick?

Say I have:
Class Image < ApplicationRecord
require 'carrierwave'
mount_uploader :specialup, SpecialupUploader
...methods...
def crop
require 'mini_magick'
...some code...
end
...methods...
end
Carrierwave uses Minimagick too and modifies its commands in some way that causes bugs in all other places that are not connected with carrierwave.
If you're intrested in specifics (and to help others to find this question in future): Carrierwave adds option -specialup(uploader's name) to all the minimagick commands (which base on mogrify). So Mogrify rolls out error messages like:
`mogrify: unrecognized option '-specialup' # error/mogrify` and so on
I tried to reload minimagick with require 'mini_magick' in the method's body but there's no result. Also tried load 'mini_magick' - the same.
I can't figure out how to make it right!

CarrierWave - delete version only, not original

Is it possible to ONLY delete versions and keep the original file?
Uploading an image creates the original file and the :product_thumb version (Works).
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :fog # Amazon S3
version :product_thumb, if: :create_thumb? do
process :resize_to_fit => [15, 15]
end
protected
def create_thumb?
model.make_thumbnail
end
end
When i run:
prod = Product.first
prod.remove_image!
prod.save
Both the original and :product_thumb get deleted. But i only want to delete the version :product_thumb. I looked through the API and didn't see anything that would help.
I've been trying to get this to work in the console.
prod.remove_product_thumb_image! --> # NoMethodError: undefined method
prod.remove_image(:product_thumb) --> # ArgumentError: wrong number of arguments (1for0)
# one idea was to get the file path of the version and delete it manually
prod.image.url(:product_thumb) --> # path/to/file/product_thumb_test.png
Is there a way to delete the version through CarrierWave?
To delete all versions use:
prod.image.remove_versions!
To delete a specific version use:
prod.image.versions[:product_thumb].remove!
As far as I could find out, there's no short command to delete an image version. However, you can build a little Ruby script that will do it for you, i.e. delete the respective files.
Step 1: Remove the image files
Use a script like this:
Product.all.each do |p|
path = File.delete(p.image.product_thumb.current_path)
File.delete(path) if File.exists?(path.to_s)
end
Step 2: Remove the image version
Remove the product_thumb version from your ImageUploader.
Done!
It will be like there had never been an image version.

Rails convert Paperclip directory structure to Carrierwave

I was using the Paperclip gem: https://github.com/thoughtbot/paperclip
I'm now using Carrierwave: https://github.com/carrierwaveuploader/carrierwave
My production website is currently using Paperclip. I'm going to be updating the production website to use Carrierwave.
The folder structure for uploads in Paperclip differs from Carrierwave.
I'm also using Amazon S3 to store uploads.
I'm wondering if there's a way to convert my production files uploaded with Paperclip to Carrierwave.
For example, with Paperclip in production I currently have something like the following for resumes:
bucket_name/model_name/resume/000/000/model_id/original/test.pdf
With Carrierwave it should be:
bucket_name/uploads/model_name/resume/model_id/original/test.pdf
Right now it seems I have to make this conversion manually. I was wondering if there's a better approach.
Please advise.
The CarrierWave::Compatibility::Paperclip module already provides this functionality. Just do the following in your uploader:
class MyUploader < CarrierWave::Uploader::Base
include CarrierWave::Compatibility::Paperclip
# The :id_partition symbol will trigger a proc in the Paperclip compatibility module that will build out the properly partition directory structure
def store_dir
"#{model.class.to_s.underscore}/#{mounted_as.to_s}/:id_partition"
end
end
Have you tried changing the store_dir options define in carrierwave uploader to look exactly like that of paperclip
def store_dir
"#{model.class.to_s.underscore}/resume/#{id_partitioning}/original/"
end
def id_partitioning
("%09d" % model.id).scan(/.{3}/).join("/")
end
Note : I just done remember how the paperclip does the id_partitioning (how much '0' it pad to the left based on object id )
but based upon your format 000/000/model_id look to me like 9 character Please confirm
Hope this help

Heroku, CarrierWave, MiniMagick: random tmp file missing

in my RoR project I am using CarrierWave + MiniMagick and deployed to Heroku for production.
I have this problem in production that sometimes the tmp file is missing for image processing. I get this error:
Errno::ENOENT: No such file or directory - /tmp/mini_magick20130319-2-3wq6l6.jpg
I have other XUploader classes that works but this particular one have two image processes. Initially I had two separate process for this:
process :resizer
def resizer
resize_to_fit(model.jrac_image_width, model.jrac_image_height)
end
process :cropper
def cropper
manipulate! do |img|
img.crop("442x190+#{model.jrac_crop_x}+#{model.jrac_crop_y}")
img
end
end
but it said I was having error on :cropper saying the tmp file doesn't exist. I tried to change the code to this, hopefully it will only work on it once:
process :resize_and_crop
def resize_and_crop
manipulate! do |img|
img.resize("#{model.jrac_image_width}x#{model.jrac_image_height}") # resize_to_fit
img.crop("442x190+#{model.jrac_crop_x}+#{model.jrac_crop_y}") # cropper
img
end
end
but unfortunately, still experiencing the same errors.
Does anyone have any idea where the problem is? I don't know if it's with Heroku or CarrierWave or ImageMagick?
Edit
I also have this code on my Uploader class
def cache_dir
"#{Rails.root}/tmp/uploads"
end
as for this document.
heroku will empty the tmp from time to time, usually, we use s3 or other cloud storage to store the processed version. You may refer to https://github.com/jnicklas/carrierwave and
https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Make-Carrierwave-work-on-Heroku
They have detailed walkthrough on using carrierwave in heroku
I have tried that before and it works

paperclip callbacks or simple processor?

I wanted to run the callback after_post_process but it doesn't seem to work in Rails 3.0.1 using Paperclip 2.3.8. It gives an error:
undefined method `_post_process_callbacks' for #<Class:0x102d55ea0>
I want to call the Panda API after the file has been uploaded. I would have created my own processor for this, but as Panda handles the processing, and it can upload the files as well, and queue itself for an undetermined duration I thought a callback would do fine. But the callbacks don't seem to work in Rails3.
after_post_process :panda_create
def panda_create
video = Panda::Video.create(:source_url => mp3.url.gsub(/[?]\d*/,''), :profiles => "f4475446032025d7216226ad8987f8e9", :path_format => "blah/1234")
end
I tried require and include for paperclip in my model but it didn't seem to matter.
Anyideas?
Solution...
I put the callback after the paperclip has_attached in the given model and it works beautifully. I was just so used to always putting the callback at the top of all models that this didn't occur to me til later.
Moving the has_attached_file attribute above the validates_presence_of and validates_attachment
in your model still needs to be done it seems. I just ran into the same problem in my Rails 4/Ruby 2 implementation of PaperClip and putting it above fixed it.
I ran into this problem because the name of my paperclip image property did not match the name I was validating against.
as_attached_file :image
validates_attachment_content_type: :not_image

Resources