Fixing attachment_fu undefined method error
February 19, 2009
Hello there. First of all apologies for not posting for a while. I have been busy working on commercial Rails projects for the very first time and as such, that takes precedence. However, I am also aware of the need to keep blogging and keep up the practise more as a note to myself about the things I have encountered through work and also to show potential exmployers that I KNOW WHAT I AM TALKING ABOUT.
To start off 2009, I would like to highlight an interesting error I came across today with regards to using attachment_fu plugin. It is a great plugin for handling file uploads in Rails and there are plenty of resources on the web to get you started. The issue arose when I fired up an old Rails application running on 2.0.2 with a view that uses attachment_fu for uplaods. The following error appears on the console:
NoMethodError in XXXController
undefined method `[]‘ for #<Enumerable::Enumerator……..>
So I upgraded to Rails 2.1 and 2.2 but the error still persisted. It transpired that it is a conflict with the verion of Ruby you have running on your system (I use a MAC running on Ruby 1.8.7.) – it appears to be an issue with Ruby 1.8.7 ’s string methods mdofications.
To fix it simply navigate to vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu.rb and change the following line:
attachment_options[:path_prefix] = attachment_options[:path_prefix]
[1..-1] if options[:path_prefix].starts_with?(‘/’)
Note that starts_with? is a String method only in 1.8.7. Save an relaunch yor application and all should be well.
I obtained the above from the following google group link:
http://groups.google.com/group/attachment_fu/browse_thread/thread/502dd4504a59c84d
Hope this helps someone – certainly helps me after hours of debugging!
P/S if the above has changed since then please let me know as I may not be the most up to date.