# Before\_filter :set\_page\_title using @item

**URL:** <https://www.ruby-forum.com/t/before-filter-set-page-title-using-item/70939>\
**Category:** Rails\
**Created:** [October 4, 2006, 3:47am UTC](https://www.ruby-forum.com/t/before-filter-set-page-title-using-item/70939 "2006-10-04T03:47:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [October 4, 2006, 3:47am UTC](https://www.ruby-forum.com/t/before-filter-set-page-title-using-item/70939/1 "2006-10-04T03:47:21Z")

</div>

I have:

before\_filter :set\_page\_title

def set\_page\_title  
@page\_title = case action\_name  
when ‘new’ then ‘Submit Item’  
when ‘list’ then ‘Items’  
when ‘show’ then “Item - #{@item.title}”  
end  
end

Is there any way to get the ‘show’ line to work? @item isn’t defined  
when the before filter runs, and an after filter happens after ‘show’ is  
apparently completely rendered (so @page\_title doesn’t appear).

Thanks,  
Joe

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [October 4, 2006, 4:01am UTC](https://www.ruby-forum.com/t/before-filter-set-page-title-using-item/70939/2 "2006-10-04T04:01:14Z")

</div>

Hi Joe,

Joe R. MUDCRAP-CE wrote:

> ```
> when 'show' then "Item - #{@item.title}"
> 
> ```
> 
> @item isn’t defined when the before filter runs

Isn’t defined? Or isn’t ‘knowable’?

Bill

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [October 4, 2006, 4:03am UTC](https://www.ruby-forum.com/t/before-filter-set-page-title-using-item/70939/3 "2006-10-04T04:03:40Z")

</div>

Bill W. wrote:

> Hi Joe,
> 
> Joe R. MUDCRAP-CE wrote:
> 
> > ```
> > when 'show' then "Item - #{@item.title}"
> > 
> > ```
> > 
> > @item isn’t defined when the before filter runs
> 
> Isn’t defined? Or isn’t ‘knowable’?
> 
> Bill

undefined – the show method does ‘@item = Item.find(params[:id])’

Joe

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [October 4, 2006, 10:09am UTC](https://www.ruby-forum.com/t/before-filter-set-page-title-using-item/70939/4 "2006-10-04T10:09:53Z")

</div>

the before\_filter is launched BEFORE calling the action (as stated by  
the  
name of the filter)

2006/10/4, Joe R. MUDCRAP-CE [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > Isn’t defined? Or isn’t ‘knowable’?

–  
Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

[www.siebert-wd.de](http://www.siebert-wd.de) - Gedanken lesen

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [October 4, 2006, 1:16pm UTC](https://www.ruby-forum.com/t/before-filter-set-page-title-using-item/70939/5 "2006-10-04T13:16:20Z")

</div>

Michael S. wrote:

> ```
> > Joe R. MUDCRAP-CE wrote:
> Joe
> 
> --
> Posted via http://www.ruby-forum.com/.
> 
> removed_email_address@domain.invalid>
> 
> www.siebert-wd.de <http://www.siebert-wd.de> - Gedanken lesen
> >
> 
> ```

Why not add another filter that instantiates @item for show (and also  
the other actions that need it, such as edit, update), and then call it  
will prepend\_before\_filter (thus ensuring it gets run before your other  
before\_filter.

HTH  
CT

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [October 4, 2006, 2:58pm UTC](https://www.ruby-forum.com/t/before-filter-set-page-title-using-item/70939/6 "2006-10-04T14:58:28Z")

</div>

Hi Joe,

Joe R. MUDCRAP-CE

> > Joe R. MUDCRAP-CE wrote:
> > 
> > > ```
> > > when 'show' then "Item - #{@item.title}"
> > > 
> > > ```
> > > 
> > > @item isn’t defined when the before filter runs
> > 
> > Isn’t defined? Or isn’t ‘knowable’?
> 
> undefined – the show method does ‘@item = Item.find(params[:id])’

I can think of at least three approaches that would work.

1. You could put an additional Item.find in the "when ‘show’ " block in  
your  
filter.
2. You could use :except on your filter and do the title assignment for  
the  
‘show’ method inside the method .
3. You could drop the before\_filter altogether and do the title  
assignments  
inside each method.

My guess is that you’re probably doing this in the first place in an  
effort  
to be DRY; putting all the page title assignment code in one place.  
Good  
instinct. Not one to be followed without thought, though.

Personally, in this situation, I’d tend toward number 3. Can you tell  
me  
why?

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [October 4, 2006, 5:32pm UTC](https://www.ruby-forum.com/t/before-filter-set-page-title-using-item/70939/7 "2006-10-04T17:32:45Z")

</div>

I too tend towards 3. It’s neater.

Vish
