Before_filter :set_page_title using @item

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

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

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

the before_filter is launched BEFORE calling the action (as stated by
the
name of the filter)

2006/10/4, Joe R. MUDCRAP-CE [email protected]:

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


Michael S. [email protected]

www.siebert-wd.de - Gedanken lesen

Michael S. wrote:

> Joe R. MUDCRAP-CE wrote:
Joe

--
Posted via http://www.ruby-forum.com/.

[email protected]>

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

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

I too tend towards 3. It’s neater.

Vish