ActionView::Template::Error: undefined method for nil in view Ruby / Rails Origin: Application & API OSI layer 7 · Application Last updated 5 Nov 2026

Rails: ActionView::Template::Error (undefined method `name' for nil:NilClass): app/views/users/show.html.erb:3

A view called a method on nil (or an undefined helper), so rendering failed — the error is wrapped in ActionView::Template::Error.

Exact message you may see ActionView::Template::Error (undefined method `name' for nil:NilClass)ActionView::Template::Error (undefined local variable or method `current_user' for #<ActionView::Base)ActionView::Template::Error (undefined method `each' for nil:NilClass)ActionView::Template::Error: undefined method for nilActionView::Template::Error (undefined method `x' for
Seen on: Ruby

Meaning

Read the line number in the view. Typical: @user not set in the controller action, a nil association (@post.author.name), or a missing helper/partial local. Use safe navigation (@post.author&.name), set instance variables in the controller, and pass locals to partials.

Common causes

  • Instance variable not assigned in the controller
  • nil association in the view
  • Partial rendered without required locals
  • Helper not available (namespaced/engine)
  • Typo in method name

⚡ Quick fix

  1. Set @user = User.find(params[:id]) in the action
  2. Use &. or try in views
  3. Pass locals: render partial: "card", locals: { user: @user }

Detailed fix by platform

Rails

  1. bin/rails runner 'p Post.first&.author'

How to diagnose

  1. Line — View line?
  2. Variable — Assigned?
  3. Association — nil?

🔧 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::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::MissingTemplate: Missing template — Rails: ActionView::MissingTemplate (Missing template users/show, application/show with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder]}) (Ruby / Rails)ActionView::MissingTe…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…undefined method for nil — Ruby: NoMethodError: undefined method `x' for nil (nil:NilClass) (Ruby / Rails)undefined method for …ActionVie…

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.