Partial Confusion


#1

Heya.
I left my Rails book at work and am having a hard time googling this
one, though it’s likely easy.

I have a smallish site running on a single master layout. The master
layout has a sidebar which has, until now been the same.

I’d like to render a particular partial in the sidebar for certain
controller/actions.

In some cases, there won’t be a partial for the sidebar.

What is the most elegant way of handling this?

I’m sure there is something simple built in, but as I said, my book
is at the office.

Thanks,
toby


#2

This is what I do to have different sidebar per controller:

application_helper.rb

def render_sidebar
if @sidebar_content
@sidebar_content
elsif
FileTest.exist?("#{controller.template_root}/#{controller.controller_name}/_sidebar.rhtml")
render :partial => ‘sidebar’
end
end

Then just call render_side inside your layout. You can extend this to
include sidebar per action.


#3

Ah, I see. Seems like a waste, though, doing filesystem lookups on
every page view. I think I’ll just write something that uses a hash
of known possibilities, or look at restructuring my view code. Thanks.