undefined method for nil Ruby / Rails Origin: Application & API OSI layer 7 · Application Last updated 14 Oct 2026

Ruby: NoMethodError: undefined method `x' for nil (nil:NilClass)

You called a method on nil — the object you expected (a record, hash value, param) wasn’t there.

Exact message you may see undefined method for nilundefined method `[]' for nilfor nil:NilClass
Seen on: Ruby

Meaning

In Rails it’s usually a find_by returning nil, a missing association, params[:x] absent, or an instance variable not set in the controller for the view.

Common causes

  • find_by/first returned nil (no matching record)
  • Association not present (user.profile is nil)
  • Missing params or hash key
  • Instance variable not set for the view

⚡ Quick fix

  1. Use find (raises 404) or handle nil explicitly
  2. Use safe navigation (&.) where nil is valid
  3. Check params with require/permit and defaults

Detailed fix by platform

Ruby

  1. ruby
    user = User.find_by(email: params[:email])
    return head :not_found unless user
    puts user.profile&.bio

How to diagnose

  1. Receiver — Which object is nil (line in trace)?
  2. Data — Does the record exist?

🔧 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):

ActionController::InvalidAuthenticityToken — Rails: ActionController::InvalidAuthenticityToken (Can't verify CSRF token authenticity) (Ruby / Rails)ActionController::Inv…ActionController::UrlGenerationError: No route matches {action...} missing required keys — Rails: ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"users", :id=>nil}, missing required keys: [:id] (Ruby / Rails)ActionController::Url…ActionView::Template::Error: undefined method for nil in view — Rails: ActionView::Template::Error (undefined method `name' for nil:NilClass): app/views/users/show.html.erb:3 (Ruby / Rails)ActionView::Template:…ActiveRecord::PendingMigrationError — Rails: ActiveRecord::PendingMigrationError — Migrations are pending. To resolve this issue, run: bin/rails db:migrate (Ruby / Rails)ActiveRecord::Pending…ActiveRecord::RecordNotFound: Couldn't find X with 'id'= — Rails: ActiveRecord::RecordNotFound: Couldn't find User with 'id'=42 (Ruby / Rails)ActiveRecord::RecordN…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 …NameError: undefined local variable or method — Ruby: NameError: undefined local variable or method 'x' for main:Object (Ruby / Rails)NameError: undefined …undefined…

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.