Disabling layout in action disables rendering of action data


I have a base layout (views/layouts/base.erb) included from
ApplicationController – layout ‘base’

This seems to work fine and all controllers automatically include that
layout. However I am now trying to disable layout for certain actions
when I do a GET on them. For example I try to GET some json data using
http://localhost:3001/admin/someaction/1.json (from within my view page
to load some json data dynamically)

The “someaction” action in admin controller is something like below

def someaction
respond_to do |format|
format.html {
format.json {
render(:layout => false) <— Doing this disables sending of
@somevar through the view
@somevar = compute_contents_of_this_var_in_json
logger.info("@somevar = " + @somevar)

In views/admin/someaction.json.erb I have a single line as below
<%= @somevar %>

This basically sends out the json data in @somevar to the browser.

The problem I am running into is that when I set rendering of layout to
false in someaction, no data gets sent out (@somevar has the needed data

  • I see that in the logs) and when I don’t turn off rendering of layout,
    I get all the HTML from my layout (as I would expect) that I don’t want.

I tried removing the layout statement from ApplicationController and
then without having to turn off layout rendering in someaction, it sends
out the right json data without the HTML.

I don’t understand why turning off rendering of layout in someaction
disables sending of the json data in the @somevar variable. Sounds like
I am missing something basic but not sure what it is. I am on 2.2.2

Thanks for your help.