# A way to build \<title\>

**URL:** <https://www.ruby-forum.com/t/a-way-to-build-title/187274>\
**Category:** Rails\
**Created:** [April 20, 2010, 11:16pm UTC](https://www.ruby-forum.com/t/a-way-to-build-title/187274 "2010-04-20T23:16:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomuhs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@tomuhs](https://www.ruby-forum.com/u/tomuhs)\
**Post date:** [April 20, 2010, 11:16pm UTC](https://www.ruby-forum.com/t/a-way-to-build-title/187274/1 "2010-04-20T23:16:40Z")

</div>

I would like to be able to easily build a string. Here is how  
I am doing it right now (I have omitted parts not relevant):

In the layout:

\<%= title %\>

Which calls:

module ApplicationHelper

```
def title
    @title_parts.join ' | '
end

```

end

and @title\_parts is build like so:

class ApplicationController \< ActionController::Base

```
before_filter :prepare_views

protected

def prepare_views
    # other stuff
    prepend_title t :pnc
end

def prepend_title item
    @title_parts ||= []
    @title_parts.insert 0, item
end

```

end

class DashboardController \< ApplicationController

```
before_filter :prepare_title

def index
    prepend_title t :home
end

protected

def prepare_title
    prepend_title t :dashboard
end

```

end

How might I clean this up? My ideal would be for each controller to be  
able to call “title” setting its part of the title, and for individual  
actions to manually prepend their title part:

class ApplicationController

```
title t :pnc

```

end

class DashboardController

```
title t :dashboard

def index
    prepend_title :home
end

```

end

Such an approach presents some problems, the first being that “t” is  
not available in the class scope. So I’m a bit stuck here.

Thomas

---

<div class="post-metadata">

**Author:** ![tomuhs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@tomuhs](https://www.ruby-forum.com/u/tomuhs)\
**Post date:** [April 21, 2010, 10:09am UTC](https://www.ruby-forum.com/t/a-way-to-build-title/187274/2 "2010-04-21T10:09:56Z")

</div>

Hi Thomas,

> How might I clean this up? My ideal would be for each controller to be  
> able to call “title” setting its part of the title, and for individual  
> actions to manually prepend their title part:

You may have a look to this screencast from Ryan B. :

> **[\#30 Pretty Page Title - RailsCasts](http://railscasts.com/episodes/30-pretty-page-title)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

Pretty clean and efficient!

Hope it helps 😉

@lex

---

<div class="post-metadata">

**Author:** ![tomuhs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@tomuhs](https://www.ruby-forum.com/u/tomuhs)\
**Post date:** [April 21, 2010, 5:13pm UTC](https://www.ruby-forum.com/t/a-way-to-build-title/187274/3 "2010-04-21T17:13:51Z")

</div>

Thanks, that seems to be the right idea. Here’s what I ultimately  
settled on…the following results in “Home | Dashboard | PNC” for a  
request to DashboardController#index.

class ApplicationController \< ActionController::Base

def self.title item  
before\_filter do |controller|  
controller.instance\_eval do  
title item  
end  
end  
end

title :pnc

def initialize  
@title\_parts = []  
super  
end

protected

def title item  
@title\_parts.insert 0, item  
end

end

module ApplicationHelper

def title  
@title\_parts.map { |part|  
t part  
}.join ’ | ’  
end

end

class DashboardController \< ApplicationController

title :dashboard

def index  
title :home  
end

end

\<%= title %\>

Thomas
