Wicked_PDF working in Rails 3.1

I recently updated one of my apps to Rails 3.1 and found that my PDF generating tests were failing because they were unable to find the CSS or images to use in the PDF.

So I started playing around, and here is what I got:

In your app, add a new initializer (or edit your existing wicked_pdf.rb initializer) and put the following:

As you can see in the code, I also replaced the background urls with correct references, this is assuming that you’re keeping your CSS images in the same location as your normal images (which I don’t see a reason why not but of course you can always tweak the script)

Possible Issues

Pow.cx

I had one issue with my generated PDF using pow.cx, my CPU would jump to 100% (on the pow process) and the page would timeout. After upgrading to the latest version of pow (0.3.2) using this command:

curl get.pow.cx | sh

I was able to see my PDFs without an issue.

Additional Sources:

http://blog.phusion.nl/2011/08/14/rendering-rails-3-1-assets-to-string/

https://github.com/mileszs/wicked_pdf/issues/48

Learn More

MongoMapper – undefined method ‘reflect_on_association’

Information Tidbit:

If you’re using reflection_on_association with ActiveRecord and need to use it with MongoMapper, here is what you want to do:

undefined method `reflect_on_association' for User:Class

ActiveRecord:
User.reflect_on_association(:posts).klass.new

MongoMapper:
User.associations[:posts].klass.new

Mongoid:

User.associations['posts'].klass.new
notice the posts is a string, it will not work with a symbol

UPDATE – (Apr 2011) Mongoid now follows ActiveRecord’s style naming:

User.reflect_on_association(:posts).klass.new

Learn More

Getting Paperclip to work with MongoMapper

Last night I was trying really hard to get MongoMapper to work with Paperclip via GridFS and failed. It seems right now Paperclip or even CarrierWave are not ready to be used with the latest MongoMapper drivers and GridFS (and I needed to use the latest drivers due to Scopify).

After giving up using GridFS, I wanted to use Paperclip with MongoMapper with the normal file system and I found this article from Ben Curtis, however with the latest version of Paperclip (version 2.3.1.1) and MongoMapper (version 0.7.6) I was unsuccessful, kept getting:

Anonymous modules have no name to be referenced by

After reading the backtrace and diving into the paperclip source code I noticed that it was trying to use the ActiveRecord.logger and since I’ve removed ActiveRecord from my app, it was failing (with the odd error).

After some tweaking of Ben’s code, I came up with this: http://gist.github.com/424158

Learn More

MongoMapper logging to Rails log

I started playing around with MongoMapper for MongoDB and I have to say I’m loving it! Somehow it just feels so much easier to work with. However, I did have an issue with my code and needed to see whether or not MongoMapper was looking for account_id in my model. Since there is no sql to look at, I was looking for a way to see what MongoMapper was sending to MongoDB. Luckily the answer was very simple, in your connection string you just have to do this:

MongoMapper.connection = Mongo::Connection.new('localhost', 27017, :logger => Rails.logger)

This little tidbit came from Zyph blog post

Learn More

uninitialized constant Rails::Railtie

A few days ago my app just stopped working, after a quick google search I found a great site that explains why:

Jamie van Dyke that pointed out that inherited_resources has now fully moved to Rails 3 and will no longer work on Rails 2.3 or lower. Now Jamie seems to point to use older versions of the gem because they changed the code for Rails 3.0, however I feel that his versions aren’t the latest version you could be running. So I wanted to add my 2 cents by showing you what I use:

In my environment.rb make sure you have both these lines in:
config.gem "inherited_resources", :version => "1.0.2"
config.gem "responders", :version => "0.4.2"

Hope that helps some of you out!

Happy coding!

Learn More

uninitialized constant Test::Unit::TestResult::TestResultFailureSupport Error

Today trying to run my RSpec tests and I get an odd error:

uninitialized constant Test::Unit::TestResult::TestResultFailureSupport (NameError)
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.4/lib/active_support/dependencies.rb:105:in `const_missing’
/usr/local/lib/ruby/gems/1.8/gems/test-unit-2.0.3/lib/test/unit/testresult.rb:28
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require’
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require’
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.4/lib/active_support/dependencies.rb:158:in `require’
….

After doing some research online I found out that the issue is caused by Shoulda and Mocha being included in my app. It seems that if you put a gem dependence for Shoulda before Mocha,  you’ll get this error, so the fix is to place the gem dependence for Mocha before Shoulda.

config.gem “rspec”, :lib => false, :version => “>=1.2.6″
config.gem “rspec-rails”, :lib => ‘spec/rails’, :version => “>=1.2.6″
config.gem “mocha”
config.gem “thoughtbot-shoulda”, :lib => “shoulda”, :source => “http://gems.github.com”

Hope that helps

UPDATE:

I hit the same error having shoulda in my environment.rb file, moving it to the test.rb (in environment folder) fixed the issue as well.

Learn More