Paperclip::AdapterRegistry::NoHandlerError (No handler found for "") [duplicate] - ruby-on-rails

This question already has answers here:
Paperclip exception : Paperclip::AdapterRegistry::NoHandlerError
(11 answers)
Closed 8 years ago.
I'm a n00b to the paperclip gem, so please excuse me, but I have spent the better part of two hours on this without progress (and I have watched the railscast on it):
My Rails app has a model with a paperclip attachment:
class Foo::Bar < ActiveRecord::Base
attr_protected :id
has_attached_file :mydocument
end
And whenever I try to submit a form with a blank file field for this attachment I get the following error:
Paperclip::AdapterRegistry::NoHandlerError: No handler found for ""
What am I to do? (I installed ImageMagick and specified Paperclip.options[:command_path] = 'C:\Program Files\ImageMagick-6.8.4-Q16' in my environment file, not that I think that should make a difference in this case.)
I've uninstalled and reinstalled paperclip. I've restarted my computer. I ran the app on a co-worker's computer, and didn't get the error. But on my machine, I still get the error.

After reading through the Paperclip source, it looks like you are running into an error in the call trace:
Foo::Bar.mydocument = document
# calls
Foo::Bar.attachment_for(:mydocument).assign(document)
# calls
file = Paperclip.io_adapters.for(document)
# calls
handler_for(document).new(document)
And it looks like the handler_for call is failing because target is an empty string or is empty-string-like.
In your controller, try printing the params object to see what is in params[:foo_bar][:mydocument]. That may lead you closer to the root issue. Things that may be the problem could include
Different attribute names between controller and view
Different attribute names between controller and model
A messed up form_tag or form_for that doesn't have the correct encoding set (try :html => {:multipart => true}).

Related

Using Carrierwave on a Devise Model

I'm trying to use carrierwave to add profile images to a user model that's handled by devise.
I've used carrierwave before with no problems but this time, when I go to start the rails server, I get the error:
/var/lib/gems/1.9.1/gems/carrierwave-0.10.0/lib/carrierwave/mount.rb:46:in `uploader_option': undefined method `validate_integrity' for :ImageUploader:Symbol (NoMethodError)
There's very little information about this out there but I did read that you could ignore some errors using :ignore_integrity_errors. After trying a few times (and getting a lot of syntax errors) I finally settled on passing that in like so:
mount_uploader :profile_image, :ImageUploader, :options => {:ignore__integrity_errors => true}
I still think my syntax is incorrect because the NoMethodError persists. Any help at all on this matter would be much appreciated!
EDIT
I reverted to the commit before I started working with carrierwave, started my server, and tried again. In my browser I'm now getting the error:
uninitialized constant CarrierWave
on this line
class ImageUploader < CarrierWave::Uploader::Base
and this error apparently happens when the routes are loaded. I'm really confused about this, I have the gem in my Gemfile and I ran bundle install.
There are a couple of things going on here. First, the reason you're seeing the uninitialized constant CarrierWave error is probably just a consequence of not having restarted the server or something silly like that. If you've properly included it in the Gemfile and bundled, you should be fine.
To your original issue, though, there are three problems. The first is that you're passing a symbol as the uploader, and you need to pass the class. So you should pass ImageUploader, not :ImageUploader. The second issue is that you must pass the options hash directly to the uploader, not inside a parent hash with an options key. The third problem is that you've included an additional underscore in the ignore_integrity_errors symbol. So you should be using this:
mount_uploader :profile_image, :ImageUploader, ignore_integrity_errors: true
It would help to know what version to rails you are using. I've had this issue before but a server restart helped out.
Also, it looks like others had this issue. Maybe try doing what some of the users did here: https://github.com/carrierwaveuploader/carrierwave/issues/399

Paperclip type validation always giving error

I recently updated to Paperclip 4 (4.1.1 to be specific) where it is necessary to validate files with validates_attachment. I tried it 3 ways, as the GitHub docs suggest:
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
and the 2 ways suggested in this thread.
All the cases give me an ActiveRecord::RecordInvalid exception saying the image's content type is invalid.
I create the image using this line of code in a Grape API:
image = ActionDispatch::Http::UploadedFile.new(params[:data])
The data it receives comes from Android using Apache HttpClient's MultipartEntityBuilder
It prints the command it runs to check the mime type which is like file -b --mime-type '/path/to/file but when I run it, it gives me image/jpeg
The class only has 2 other lines:
has_attached_file :image
attr_accessible :image
And ideas why does it constantly fail?
I went into the same issue than you (even tried the other ways the thread suggests), and Rachel is right about the version. Paperclip 4 has some issue with spoofing. Changing it to 3.5 solved my problem.
Btw, if you still want to make it work, it seems to be the issue discussed here ( with a workaround given )
You might want to try doing some testing to eliminate the variables of what's going wrong here: can you try a different version of Paperclip, ~3.5? Paperclip 4 still has some bugs to be worked out (I have noticed this in my own project, it thought my normal upload had a spoofed content type).
Or: can you try creating the image a different way, just to test? The validation code you provided looks correct.

uninitialized constant UploadsController::Bucket when trying to write general AWS::S3 Upload controller

I'm trying to write a general Amazon S3 uploader (will be used mostly for images) for my rails project. I was able to set up the environment in my console following http://amazon.rubyforge.org/.
I was able to follow the guide in the console. However, I had trouble when I applied it to my rails project. When I try to access my new view, I get the following error:
NameError in UploadsController#new
uninitialized constant UploadsController::Bucket
Here is my controller:
class UploadsController < ApplicationController
require 'aws/s3'
def new
photo_bucket = Bucket.find('photos')
#photos = photo_bucket.objects
end
def create
file = 'black-flowers.jpg'
S3Object.store(file, open(file), 'photos')
end
end
In my controller, my new action will contain the form for the upload and a list of all the photos.
My create action will just save the file. I haven't figured out how the params from the form will be passed into the controller yet, so ignore the 'black-flowers.jpg' line.
My guess is that I did not establish a connection in the controller.
How do I establish a connection or fix this error?
Thanks for looking.
Bucket is not a top level constant in this case. You probably want the AWS::S3::Bucket constant for new, and I'd assume something similar for S3Object.
Note that you may also want to look into the Fog library for this.
The fact that you haven't figured out how params will be passed in implies that you may also want to work through the Rails tutorials without S3 first.
I had a similar issue and it was solved by just checking all files required were provided and restarting the server

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

How to fix / debug 'expected x.rb to define X.rb' in Rails

I have seen this problem arise in many different circumstances and would like to get the best practices for fixing / debugging it on StackOverflow.
To use a real world example this occurred to me this morning:
expected announcement.rb to define Announcement
The class worked fine in development, testing and from a production console, but failed from in a production Mongrel. Here's the class:
class Announcement < ActiveRecord::Base
has_attachment :content_type => 'audio/mp3', :storage => :s3
end
The issue I would like addressed in the answers is not so much solving this specific problem, but how to properly debug to get Rails to give you a meaningful error as expected x.rb to define X.rb' is often a red herring...
Edit (3 great responses so far, each w/ a partial solution)
Debugging:
From Joe Van Dyk: Try accessing the model via a console on the environment / instance that is causing the error (in the case above: script/console production then type in 'Announcement'.
From Otto: Try setting a minimal plugin set via an initializer, eg: config.plugins = [ :exception_notification, :ssl_requirement, :all ] then re-enable one at a time.
Specific causes:
From Ian Terrell: if you're using attachment_fu make sure you have the correct image processor installed. attachment_fu will require it even if you aren't attaching an image.
From Otto: make sure you didn't name a model that conflicts with a built-in Rails class, eg: Request.
From Josh Lewis: make sure you don't have duplicated class or module names somewhere in your application (or Gem list).
That is a tricky one.
What generally works for me is to run "script/console production" on the production server, and type in:
Announcement
That will usually give you a better error message. But you said you already tried that?
I just ran into this error as well.
The short of it was that my rb file in my lib folder was not in a folder structure to match my module naming convention. This caused the ActiveSupport auto loader to use the wrong module to see if my class constant was defined.
Specifically I had defined the following class
module Foo
class Bar
end
end
In the root of /lib/bar.rb
This caused the autoloader to ask module Object if Bar was defined instead of module Foo.
Moving my rb file to /lib/foo/bar.rb fixed this problem.
I've encountered this before, and the AttachmentFu plugin was to blame. I believe in my case it was due to AttachmentFu expecting a different image processor than what was available, or non-supported versions were also installed. The problem was solved when I explicitly added :with => :rmagick (or similar -- I was using RMagick) to the has_attachment method call even for non-image attachments. Obviously, make sure that your production environment has all the right gems (or freeze them into your application) and supporting software (ImageMagick) installed. YMMV.
As for not getting Rails and AttachmentFu to suck up and hide the real error -- we fixed it before figuring it out completely.
Since this is still the top Google result, I thought I'd share what fixed the problem for me:
I had a module in the lib folder with the exact same name as my application. So, I had a conflict in module names, but I also had a conflict of folder names (not sure if the latter actually makes a difference though).
So, for the OP, make sure you don't have duplicated class or module names somewhere in your application (or Gem list).
For me, the cause was a circular dependency in my class definitions, and the problem only showed up using autotest in Rails. In my case, I didn't need the circular dependency, so I simply removed it.
You can try disabling all your plugins and add them back in one by one.
In environment.rb in the Initalizer section, add a line like this one:
config.plugins = [ :exception_notification, :ssl_requirement, :all ]
Start with the minimum set to run your application and add them in one by one. I usually get this error when I've defined a model that happens to map to an existing filename. For example, a Request model but Rails already has a request.rb that gets loaded first.
I had this problem for a while and in my case the error was always preceded from this S3 error:
(AWS::S3::Operation Aborted) "A
conflicting conditional operation is
currently in progress against this
resource. Please try again."
This problem usually occurs when creating the same bucket over and over again. (Source AWS Developers forum)
This was due to the fact that I had used attachment_fu to create the bucket and I had decommented the line containing the command Bucket.create(##bucket_name) in lib/technoweenie/attachment_fu/backends/s3_backends.rb (near to line 152).
Once commented or deleted the command Bucket.create(##bucket_name) the problem disappeared.
I hope this helps.
Changing class names while using STI caused this for me:
Class changed from 'EDBeneficiary' to 'EdBeneficiary'
Existing records had 'EDBeneficiary' stored in the 'type' column, so when Rails tried to load them up the exception was raised.
Fix: Run a migration to update values in the 'type' column to match the new class name.
in my case, I am getting this error in the development console but I can load the class in irb
Sorry this isn't a definitive answer, but another approach that might work in some specific circumstance:
I just ran in to this problem while debugging a site using Ruby 1.8.7 and Merb 1.0.15. It seemed that the class in question (let's call it SomeClass) was falling out of scope, but when some_class.rb file was automatically loaded, the other files it required (some_class/base.rb etc) were not loaded by the require mechanism. Possibly a bug in require?
If I required some_class file earlier, such as the end of environment.rb, it seems to prevent the object falling out of scope.
I was getting this error duo to a controller definition being in a file that wasn't named as a controller. For instance, you have a Comment model and you define the controller in a comment.rb file instead of comments_controller.rb
I had this problem with rails version 1.2.3. I could reproduce the problem only with mongrel, using console environment access didn't give any useful info. In my case, I solved making the RAILS_ROOT/html folder writable by mongrel and then restarting the web server, as some users reported here:
http://www.ruby-forum.com/topic/77708
When I upgraded rails from 1.1.6 to 1.2.6 and 2.0.5 for my app, I faced this error. In short, old plugins caused this error. These plugins were already out-dated and no update anymore (even no repo!). After I removed them, the app worked on 1.2.6 and 2.0.5. But I didn't check the detail source code of the plugins.

Resources