# Layout that loads only for specific actions

**URL:** <https://www.ruby-forum.com/t/layout-that-loads-only-for-specific-actions/83938>\
**Category:** Rails\
**Created:** [January 30, 2007, 9:53pm UTC](https://www.ruby-forum.com/t/layout-that-loads-only-for-specific-actions/83938 "2007-01-30T21:53:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![colemanm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb9ed0/32.png) [@colemanm](https://www.ruby-forum.com/u/colemanm)\
**Post date:** [January 30, 2007, 9:53pm UTC](https://www.ruby-forum.com/t/layout-that-loads-only-for-specific-actions/83938/1 "2007-01-30T21:53:17Z")

</div>

Is there an easy way to have a piece of code or even an entire layout  
that not only loads for a given controller, but only a given action on  
that controller? Specifically, I’m trying to load a small Google Map  
using their API, and I’ve written it to automatically geocode the  
address based on an address value passed from the controller.

I have a controller called “events” that lists a bunch of upcoming  
events, and when you select an event, a details page for that event is  
invoked via the “details” action. The API only gets passed the address  
for geocoding when “details” is loaded, but in its current state, it  
also wants the address everywhere in the events controller, and since  
it’s “nil” Rails gives me an error. I want to be able to only load the  
map from the Javascript code when one action is invoked, not when the  
whole “events” view is loaded. Is there an easy way to do this with  
layouts?

(Sorry for the roundabout question, as I’m something of a newb when it  
comes to programming and a total newb when it comes to RoR).

Thanks!

- Coleman

---

<div class="post-metadata">

**Author:** ![colemanm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb9ed0/32.png) [@colemanm](https://www.ruby-forum.com/u/colemanm)\
**Post date:** [January 30, 2007, 10:04pm UTC](https://www.ruby-forum.com/t/layout-that-loads-only-for-specific-actions/83938/2 "2007-01-30T22:04:55Z")

</div>

If I’m understanding you right, you should be able to just do:  
render :layout =\> ‘events’

Put that in the controller method for the action you want the layout  
loaded for. Any other actions will still use the default layout for  
that controller

- Paul

On Jan 30, 3:53 pm, Coleman Mccormick \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![colemanm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb9ed0/32.png) [@colemanm](https://www.ruby-forum.com/u/colemanm)\
**Post date:** [January 30, 2007, 10:12pm UTC](https://www.ruby-forum.com/t/layout-that-loads-only-for-specific-actions/83938/3 "2007-01-30T22:12:16Z")

</div>

crayz wrote:

> If I’m understanding you right, you should be able to just do:  
> render :layout =\> ‘events’
> 
> Put that in the controller method for the action you want the layout  
> loaded for. Any other actions will still use the default layout for  
> that controller
> 
> - Paul
> 
> On Jan 30, 3:53 pm, Coleman Mccormick \<rails-mailing-l…@andreas-

You’re exactly right. I was just about to reply that I figured it out.  
Pretty simple solution. Thanks a lot!
