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

**Code:** `NameError: undefined local variable or method` · **Category:** Ruby / Rails · **Seen on:** Ruby

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

## 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?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/ruby/nameerror-undefined-local-variable-or-method)
