undefined method `parent' for nil:NilClass-Collection of common programming errors

I’m getting this strange error using Rails 3.0.2.

ActionView::Template::Error (undefined method `parent' for nil:NilClass):
app/controllers/channels_controller.rb:19:in `index'

This is the controller, and line 19 is the respond_with(@channels) block.

Where do I start to search for errors?

class ChannelsController < ApplicationController
  before_filter :set_default_client
  respond_to :html, :xml

  def index
    if params[:cache_set]
      @channels = Channel.active.find_all_by_id(params[:cache_set])
    else
      @channels = Channel.active.find_all_by_id(cookies[:channels].split(','))
    end

    respond_with(@channels)
  end
end

This is the full error:

ActionView::Template::Error (undefined method `parent' for nil:NilClass):
  app/controllers/channels_controller.rb:19:in `index'

Rendered /Users/linus/.rvm/gems/ruby-1.8.7-p330/gems/actionpack-3.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.3ms)
Rendered /Users/linus/.rvm/gems/ruby-1.8.7-p330/gems/actionpack-3.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (19.8ms)
Rendered /Users/linus/.rvm/gems/ruby-1.8.7-p330/gems/actionpack-3.0.2/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (28.6ms)

I’m using Ruby 1.8.7 with Rails 3.0.2. I’ve also, just in case, tried Rails 3.0.7 and 3.0.0.

  1. I solved the problem by changing HAML version from 3.1.x to 3.0.24.

    My new Gemfile looks like this.

    gem "rails", "3.0.2"
    gem "haml", "3.0.24"
    gem "compass", "0.10.6"
    
  2. Are you in any case using HAML?

    I just bumped into this as well and my colleague (who is not on Stackoverflow yet) found out that it was due to multiline comments in the view template

    -#
      = helper_method_1
      = helper_method_2
    
  3. It looks like HAML-edge has fixed this problem. I expect the next release of HAML after 3.1.1 to resolve this.

Originally posted 2013-11-09 22:30:32.