NameError: undefined local variable or method Ruby / Rails Origin: Application & API OSI layer 7 · Application Last updated 5 Nov 2026

Ruby: NameError: undefined local variable or method 'x' for main:Object

Ruby saw a bare identifier that is neither a local variable nor a method on the current object.

Exact message you may see NameError: undefined local variable or method 'x' for main:Objectundefined local variable or method for #<Class>NameError: undefined local variable or method `foo' for #<User>undefined local variable or method `current_user' for #<ApplicationController>NameError (undefined local variable or method)
Seen on: Ruby

Meaning

Typical: a typo, a variable defined in another scope (blocks, methods don't share locals), a helper method not available in that class/view, or calling an instance method from a class method. Check the receiver named after "for" in the message.

Common causes

  • Typo in a variable/method name
  • Variable defined in another method/block scope
  • Helper not included in that object
  • Instance method called in a class method (self.)
  • Missing attr_reader/method definition

⚡ Quick fix

  1. Check scope and spelling
  2. Use self. or instance_variable correctly
  3. Include/extend the module that defines the helper

Detailed fix by platform

Ruby

  1. defined?(x) # => nil if undefined

How to diagnose

  1. Receiver — Object named in message?
  2. Scope — Defined where?
  3. Helper — Included?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

ActiveRecord::AssociationTypeMismatch — Rails: ActiveRecord::AssociationTypeMismatch: User(#1234) expected, got "42" which is an instance of String(#5678) (Ruby / Rails)ActiveRecord::Associa…ActiveRecord::ConnectionNotEstablished — Rails: ActiveRecord::ConnectionNotEstablished: No connection pool for 'ActiveRecord::Base' found (Ruby / Rails)ActiveRecord::Connect…ActiveRecord::Deadlocked: PG::TRDeadlockDetected — Rails: ActiveRecord::Deadlocked: PG::TRDeadlockDetected: ERROR: deadlock detected (Ruby / Rails)ActiveRecord::Deadloc…ActiveRecord::IrreversibleMigration — Rails: ActiveRecord::IrreversibleMigration — This migration uses remove_column without a type; db:rollback can't reverse it (Ruby / Rails)ActiveRecord::Irrever…ArgumentError: missing keyword / unknown keyword — Ruby: ArgumentError: missing keyword: :name / unknown keyword: :nmae (Ruby / Rails)ArgumentError: missin…ArgumentError: wrong number of arguments (given 1, expected 2) — Ruby: ArgumentError: wrong number of arguments (given 1, expected 2..3) (Ruby / Rails)ArgumentError: wrong …FrozenError: can't modify frozen String — Ruby: FrozenError: can't modify frozen String: "abc" (Ruby / Rails)FrozenError: can't mo…NameError: uninitialized constant — Ruby: NameError: uninitialized constant UsersController::User (Did you mean? Users) (Ruby / Rails)NameError: uninitiali…NameError…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.