# Different \<title\> per action, when using layouts?

**URL:** <https://www.ruby-forum.com/t/different-title-per-action-when-using-layouts/71648>\
**Category:** Rails\
**Created:** [October 10, 2006, 4:31pm UTC](https://www.ruby-forum.com/t/different-title-per-action-when-using-layouts/71648 "2006-10-10T16:31:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![blacknight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@blacknight](https://www.ruby-forum.com/u/blacknight)\
**Post date:** [October 10, 2006, 4:31pm UTC](https://www.ruby-forum.com/t/different-title-per-action-when-using-layouts/71648/1 "2006-10-10T16:31:45Z")

</div>

Hey everyone,

Right now I wrap all my pages in an application-level layout. It writes  
the header, the encompassing body, etc. I’d like to have a default value  
for the that could be overridden by a controller method. All I  
can think of is defining a @title variable for every controller method  
– and it seems redundant. What’s the best way to go about this?

Thanks,  
Ben

---

<div class="post-metadata">

**Author:** ![blacknight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@blacknight](https://www.ruby-forum.com/u/blacknight)\
**Post date:** [October 10, 2006, 4:49pm UTC](https://www.ruby-forum.com/t/different-title-per-action-when-using-layouts/71648/2 "2006-10-10T16:49:11Z")

</div>

Ben W. wrote:

> Hey everyone,
> 
> Right now I wrap all my pages in an application-level layout. It writes  
> the header, the encompassing body, etc. I’d like to have a default value  
> for the that could be overridden by a controller method. All I  
> can think of is defining a @title variable for every controller method  
> – and it seems redundant. What’s the best way to go about this?
> 
> Thanks,  
> Ben

My apologies – I found a thread that had exactly what I wanted.

[http://www.ruby-forum.com/topic/63761#69833](http://www.ruby-forum.com/topic/63761#69833)

Thanks,  
Ben

---

<div class="post-metadata">

**Author:** ![blacknight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@blacknight](https://www.ruby-forum.com/u/blacknight)\
**Post date:** [October 10, 2006, 5:33pm UTC](https://www.ruby-forum.com/t/different-title-per-action-when-using-layouts/71648/3 "2006-10-10T17:33:44Z")

</div>

On 10/10/06, Ben W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hey everyone,
> 
> Right now I wrap all my pages in an application-level layout. It writes  
> the header, the encompassing body, etc. I’d like to have a default value  
> for the that could be overridden by a controller method. All I  
> can think of is defining a @title variable for every controller method  
> – and it seems redundant. What’s the best way to go about this?

I think you can simply use

\<%= controller.action\_name %\>

or \<%= controller.controller\_name %\>

etc.

If you want per-controller optional overrides, try:

\<%= @title || controller.controller\_name %\>

Then just define @title for the controllers that you want to have the  
layout use something other than the controller name.

---

<div class="post-metadata">

**Author:** ![blacknight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@blacknight](https://www.ruby-forum.com/u/blacknight)\
**Post date:** [October 10, 2006, 5:35pm UTC](https://www.ruby-forum.com/t/different-title-per-action-when-using-layouts/71648/4 "2006-10-10T17:35:44Z")

</div>

Ben W. \<rails-mailing-list@…\> writes:

> All I can think of is defining a title  
> variable for every controller method  
> – and it seems redundant. What’s the best way to go about this?

Well, at the end of the day you’re going to need _something_ which is  
different  
for every title you want to have, so I would think that per-method would  
be ideal

---

<div class="post-metadata">

**Author:** ![blacknight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c68b51/32.png) [@blacknight](https://www.ruby-forum.com/u/blacknight)\
**Post date:** [October 10, 2006, 9:47pm UTC](https://www.ruby-forum.com/t/different-title-per-action-when-using-layouts/71648/5 "2006-10-10T21:47:00Z")

</div>

I’m using this:

Default Prefix - \<%= $title || controller.action\_name %\>

if you want to change the title for some controller just overwrite  
$title for this controller e.g. in the initialize method

Daniel
