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.
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) 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
- Check scope and spelling
- Use
self.orinstance_variablecorrectly - Include/extend the module that defines the helper
Detailed fix by platform
Ruby
defined?(x) # => nil if undefined
How to diagnose
- Receiver — Object named in message?
- Scope — Defined where?
- 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:
Similar errors
- ActiveRecord::AssociationTypeMismatch Rails: ActiveRecord::AssociationTypeMismatch: User(#1234) expected, got "42" which is an instance of String(#5678)
- ActiveRecord::ConnectionNotEstablished Rails: ActiveRecord::ConnectionNotEstablished: No connection pool for 'ActiveRecord::Base' found
- ActiveRecord::Deadlocked: PG::TRDeadlockDetected Rails: ActiveRecord::Deadlocked: PG::TRDeadlockDetected: ERROR: deadlock detected
Most viewed in Ruby / Rails
- ActiveRecord::PendingMigrationError Rails: ActiveRecord::PendingMigrationError — Migrations are pending. To resolve this issue, run: bin/rails db:migrate
- ActionController::InvalidAuthenticityToken Rails: ActionController::InvalidAuthenticityToken (Can't verify CSRF token authenticity)
- CORS blocked in Rails API (rack-cors) Browser: Access to fetch at 'http://localhost:3000/api/items' from origin 'http://localhost:5173' has been blocked by CORS policy — Rails
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 5 Nov 2026