Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method `parent' for nil:NilClass

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.

like image 732
Linus Oleander Avatar asked May 06 '11 00:05

Linus Oleander


People also ask

What does undefined method for nil NILClass mean?

The Undefined method for nil:NILClass occurs when you attempt to use a formula on a blank datapill. This indicates that the datapill was not provided any value at runtime.

What does it mean when you get this error message undefined method user for NILClass?

Explained. This is a common Ruby error which indicates that the method or attribute for an object you are trying to call on an object has not been defined.

What is an undefined method in Ruby?

What is an undefined method in Ruby? Undefined method call created a NoMethodError. This is a typical Ruby error that indicates that the method or attribute you are attempting to call on an object has not been declared.

Does Ruby have undefined?

Undefined instance variables are always nil , so you want to check for that. Try the “safe navigator operator” (Ruby 2.3+) which only calls a method if the variable is not nil .


1 Answers

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
like image 113
Jevado Avatar answered Nov 08 '22 18:11

Jevado