Rails 2 -> 3 undefined method `html_safe’ for nil:NilClass error

I am converting the Recycling Group Finder site from Rails 2 to Rails 3 and though it has mostly gone to plan I was temporarily held up by this error which I was getting on some pages:

The error was hard to track down as the error message wasn’t very descriptive, but in the end it turned out to be caused by a comment. I am using content_for blocks to generate sections of page content and for a long section I had added a comment to the end of the block to help be know which block was closing:

It turns out that this ‘# some_section’ comment was the problem, possibly because of the change to erubis in Rails 3. Removing the comment caused the page to start working again:

I hope this page helps short-cut the debugging for anyone else that is bitten by this issue.

Tagged , ,

4 thoughts on “Rails 2 -> 3 undefined method `html_safe’ for nil:NilClass error

  1. Tony says:

    A bit off-topic, but I’m curious about your use of Dropbox here — do you use it to sync your local apps between machines, or something?

    • Will says:

      That’s correct, I use DropBox to sync selected apps between my laptop and iMac. Just the ones I will be working on on my laptop, the rest stay where they are on my iMac. It’s worked flawlessly so far.

  2. Blake says:

    Thanks a lot for the post! This error is a tricky one, I think you just saved me a massive headache. Seems like this is quite a dangerous bug to me.

  3. lolmaus says:

    Hey, i did exatly the same comment style and got this error. You saved me out, thanks! ^_^

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s