Topic: "NameError: undefined local variable or method `config' for" error


I am just learning ruby and rails and I am trying to refactor some code and have run into a problem that has me stumped and that I think may just be a configuration problem.  I installed Ruby and Rails as well as a few gems and when I run rspec on the code I get the following failure:

"NameError:  undefined local variable or method `config' for #<collector:0x3955140>"

The line of code that is tied to the error above is:

"return image_tag("default.png")"

I have read through a bit of documentation trying to figure out what my problem may be and have found that image_tag is in the module ActionView::Helpers::AssetTagHelper and I think I installed the actionview module correctly, could this error indicate I have not done so?

I also suspect I may need to do more to setup the project I am working on, all that I have done so far is have a directory with my ruby script .rb files and then my spec file I run rspec on to refactor the code.  Do I need to setup more in my project and if so what?

I am sorry if these are noob questions but I definitely am a beginner and after searching quite a while I am still stuck.  Thank you for the help.

Re: "NameError: undefined local variable or method `config' for" error

Posting some code would definitely help, particularly the file the contains the code that starts the error stack. If you're making a rails application, you should always start with:

rails new myapp

myapp can be renamed to whatever you want to  call the application. That script creates the files and folders as required to get started.

Re: "NameError: undefined local variable or method `config' for" error

Thank you for the reply, it is most appreciated.  Okay, I did not do the "rails new myapp" command, so that is definitely one problem.  I have been doing C/C++ application development for the last 6+ years so I have a decent programming background and am trying to learn ruby on my own via web resources and definitely need to learn more of the unique little things to ruby such as this command that needs to be run before starting an app.  I am very used to just working with compiled code and occasionally writing PERL scripts so interpreted languages are definitely not something I have a vast amount of experience with.

Anyway, I am going to go use that command in a bit and see if I can get farther with the refactoring and in the meantime if it helps here is the relevant code from the file containing the class that the error mentioned above is coming out of:

require 'rubygems'
require 'action_view'

class Collector

  include ActionView::Helpers::TagHelper
  include ActionView::Helpers::AssetTagHelper


  def display(profile, size, html = {}, options = {}, link = true)
    ... unrelated code here ...
    return image_tag("default.png")


Last edited by danga (2011-01-16 19:48:24)

Re: "NameError: undefined local variable or method `config' for" error

I ran the command "rails new refactored" and then copied my ruby scripts I am refactoring into the "app" sub-directory in the new project and when I run "rspec spec.rb" I still get the error mentioned in my original post.  Basically I have a few short ruby script files containing a few very basic classes and then a specification file to use for refactoring with "rspec" and that is ALL and I really need to get done learning how to refactor the code.  So I need to configure this project starting only with those files and I think that may be part of the reason this is difficult for a newbie like me.

Also, since I didn't specifically mention this in my original post I am running the command "rspec spec.rb" and getting the error below which is coming out of a file containing the code in my previous post containing the "Collector" class:

"NameError:  undefined local variable or method `config' for #<collector:0x3955140>"

The specific line of code that the error is tied to is:

"return image_tag("default.png")"

I really don't understand how an error talking about an undefined variable or method named "config" could be tied to the line above that just returns the result of the image_tag method.  What all should I need to configure in order to use the "image_tag" function found in the module "ActionView::Helpers::AssetTagHelper"?

Re: "NameError: undefined local variable or method `config' for" error

I eliminated the error by adding: require 'action_view/helpers/asset_tag_helper'

Last edited by danga (2011-01-17 22:35:34)

Re: "NameError: undefined local variable or method `config' for" error

Quick note that I have run into this error in a helper unit test in an upgrade from Rails 2.3.11 to 3.0.4.  It was due to a combination of not inheriting from ActionView::Test and calling `super` in the setup method.