Forum: Ruby on Rails Got an error on returning data as json format when the data is nil

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
wolfgang (Guest)
on 2009-01-11 03:30
(Received via mailing list)
Hi there,

One of my app's actions returns data as a json format.
If the data exists, everything works perfect.
But if the data is nil, I get the following error.
ActionView::MissingTemplate (Missing template user/show.erb in view
path /var/www/my_app/releases/20090110234348/app/views:):
template.rb:95:in `find_full_path'
template.rb:16:in `initialize'
base.rb:332:in `new'
base.rb:332:in `_unmemoized__pick_template'
active_support/memoizable.rb:70:in `_pick_template'

I don't know why rails requires a template file only when the data is
nil. Anybody knows the reason ?

Here is a snippet from the controller.
class UserController < ApplicationController

  def show
    name = params[:name]

    person = User.find_by_name(name)

    respond_to do |format|
      format.html # show.html.erb
      format.json  { render :json => person }


How can I avoid this situation ???

Thanks in advance.

Robert W. (Guest)
on 2009-01-12 01:39
wolfgang wrote:
> How can I avoid this situation ???
I don't know specifically why Rails generates that particular error, but
that doesn't really matter, because render :json => nil is not valid.
You need to check for nil and respond with a properly constructed error
response, including the proper HTTP error status. I think most people
response with a 404 - Page not found in this case, but I'm not
completely sure of that.
wolfgang (Guest)
on 2009-01-13 23:06
(Received via mailing list)
Hi Robert,

Thanks for reply.

So does it mean JSON cannot represent a single nil object ? serious !?

Ajax calls the action and it can treat the error as nil object as you
but isn't it ugly ???

thanks in advance.


On Mon, Jan 12, 2009 at 8:39 AM, Robert W.
This topic is locked and can not be replied to.