# Overriding layout causes empty response with content\_for

**URL:** <https://www.ruby-forum.com/t/overriding-layout-causes-empty-response-with-content-for/153686>\
**Category:** Rails\
**Created:** [November 20, 2008, 4:44am UTC](https://www.ruby-forum.com/t/overriding-layout-causes-empty-response-with-content-for/153686 "2008-11-20T04:44:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![darth\_ivan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@darth\_ivan](https://www.ruby-forum.com/u/darth_ivan)\
**Post date:** [November 20, 2008, 4:44am UTC](https://www.ruby-forum.com/t/overriding-layout-causes-empty-response-with-content-for/153686/1 "2008-11-20T04:44:22Z")

</div>

Hi,

I’m trying to pinpoint what the issue might be here. In a controller  
that has the appropriate layout (layouts/controller.html.erb), if I try  
to override the layout to the ‘application’ layout only for certain  
actions, those actions do get rendered with the application layout…  
Like this:

layout ‘application’, :only =\> :new

The ‘new’ action renders fine, but now the other actions don’t render at  
all!

No error or anything, just an empty response. I’ve tried renaming the  
layout files to see if an error shows up, and no error (so no layout is  
being picked up).

On my views I have content\_for, so since nothing is yielding those  
contents, nothing shows up. If I put something outside a content\_for, it  
does show up (without a layout of course).

If I explicitly render the layout on the action, it works, like so:

render :layout =\> ‘somelayout’

So what’s the deal here?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![darth\_ivan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@darth\_ivan](https://www.ruby-forum.com/u/darth_ivan)\
**Post date:** [November 20, 2008, 4:54am UTC](https://www.ruby-forum.com/t/overriding-layout-causes-empty-response-with-content-for/153686/2 "2008-11-20T04:54:48Z")

</div>

I would try to explicitly specify the layout for the other actions. If  
you  
read the docs, you can sort of infer that no layout will be applied if  
you  
explicitly apply a layout to some actions:  
[http://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html](http://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html)

“This will assign “weblog\_standard” as the WeblogController’s  
layout[http://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html#M000301](http://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html#M000301)except  
for the  
rss action, which will not wrap a  
layout[http://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html#M000301](http://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html#M000301)around  
the rendered view.”

Not sure that’s the right answer – just offering.

jim  
[jim-rants.com](http://jim-rants.com)

On Wed, Nov 19, 2008 at 10:44 PM, Ivan V.

---

<div class="post-metadata">

**Author:** ![darth\_ivan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@darth\_ivan](https://www.ruby-forum.com/u/darth_ivan)\
**Post date:** [November 20, 2008, 5:11am UTC](https://www.ruby-forum.com/t/overriding-layout-causes-empty-response-with-content-for/153686/3 "2008-11-20T05:11:08Z")

</div>

After unsuccessfully trying an after\_filter to do the render, I did  
found out that it is rendering with render\_with\_no\_layout… I was  
trying to avoid having an explicit render on every action, and now with  
your comment as I re-read the docs, I think I found an oh-so-ugly  
solution:

layout proc{ |controller| [ ‘new’, ‘etc’  
].include?(controller.action\_name) ? ‘application’ : ‘otherlayout’ }

Thanks

James E. wrote:

> I would try to explicitly specify the layout for the other actions. If  
> you  
> read the docs, you can sort of infer that no layout will be applied if  
> you  
> explicitly apply a layout to some actions:  
> [http://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html](http://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html)
> 
> “This will assign “weblog\_standard” as the WeblogController’s  
> layout[http://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html#M000301](http://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html#M000301)except  
> for the  
> rss action, which will not wrap a  
> layout[http://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html#M000301](http://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html#M000301)around  
> the rendered view.”
> 
> Not sure that’s the right answer – just offering.
> 
> jim  
> [jim-rants.com](http://jim-rants.com)
> 
> On Wed, Nov 19, 2008 at 10:44 PM, Ivan V.

---

<div class="post-metadata">

**Author:** ![darth\_ivan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@darth\_ivan](https://www.ruby-forum.com/u/darth_ivan)\
**Post date:** [November 20, 2008, 5:16am UTC](https://www.ruby-forum.com/t/overriding-layout-causes-empty-response-with-content-for/153686/4 "2008-11-20T05:16:27Z")

</div>

Ivan V. wrote:

> Hi,
> 
> I’m trying to pinpoint what the issue might be here. In a controller  
> that has the appropriate layout (layouts/controller.html.erb), if I try  
> to override the layout to the ‘application’ layout only for certain  
> actions, those actions do get rendered with the application layout…  
> Like this:
> 
> layout ‘application’, :only =\> :new
> 
> The ‘new’ action renders fine, but now the other actions don’t render at  
> all!
> 
> No error or anything, just an empty response. I’ve tried renaming the  
> layout files to see if an error shows up, and no error (so no layout is  
> being picked up).
> 
> On my views I have content\_for, so since nothing is yielding those  
> contents, nothing shows up. If I put something outside a content\_for, it  
> does show up (without a layout of course).
> 
> If I explicitly render the layout on the action, it works, like so:
> 
> render :layout =\> ‘somelayout’
> 
> So what’s the deal here?
> 
> Thanks in advance!

Try this instead: specify a default controller-wide layout, and then  
action-specific layouts defined within the action itself via render.

class MyController \< ActionController::Base  
layout “default\_layout”

```
def foo
  render :action => "foo", :layout => "foo_layout"
end

```

end

[http://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html#M000301](http://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html#M000301)

---

<div class="post-metadata">

**Author:** ![darth\_ivan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@darth\_ivan](https://www.ruby-forum.com/u/darth_ivan)\
**Post date:** [November 20, 2008, 6:58am UTC](https://www.ruby-forum.com/t/overriding-layout-causes-empty-response-with-content-for/153686/5 "2008-11-20T06:58:46Z")

</div>

Hello - have you tried looking at Ryan B. raiscasts - he has a  
couple on layouts that might help

> **[Ruby on Rails Screencasts - RailsCasts](http://railscasts.com/?tag_id=4)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

Owen

On Nov 19, 8:16 pm, Brian P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
