# Changing sidebar in layout per controller

**URL:** <https://www.ruby-forum.com/t/changing-sidebar-in-layout-per-controller/86741>\
**Category:** Rails\
**Created:** [March 24, 2006, 12:20pm UTC](https://www.ruby-forum.com/t/changing-sidebar-in-layout-per-controller/86741 "2006-03-24T12:20:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [March 24, 2006, 12:20pm UTC](https://www.ruby-forum.com/t/changing-sidebar-in-layout-per-controller/86741/1 "2006-03-24T12:20:55Z")

</div>

I am currently using 2 layouts in my site. One is for the admin area. I  
am trying to get content in my sidebar to change depending upon the  
current controller.

Example:

Within admin I have a users controller and a articles controller. When  
in the users view I want the sidebar to display options (like add user  
delete user etc).

What is the best actionview way to do this. I don’t want loads of  
differing layouts as there could be many different controllers in  
future.

I could use render partial but then in each of the controllers actions I  
would have a load of repeated html breaking the DRY rules.

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [March 24, 2006, 1:41pm UTC](https://www.ruby-forum.com/t/changing-sidebar-in-layout-per-controller/86741/2 "2006-03-24T13:41:34Z")

</div>

I accomplished this exact same task by creating a method in my  
application helper. This method would dynamically create my navbar  
based on a context variable that I would set in each template. (\<%  
@context = ‘about’ %\>). I would call my application helper method in my  
layout and let it return my modified navbar. This solution works great  
for me.

On Fri, 2006-03-24 at 12:20 +0100, James W. wrote:

> What is the best actionview way to do this. I don’t want loads of  
> differing layouts as there could be many different controllers in  
> future.
> 
> I could use render partial but then in each of the controllers actions I  
> would have a load of repeated html breaking the DRY rules.

Charlie B.  
[http://www.recentrambles.com](http://www.recentrambles.com)

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [March 24, 2006, 2:04pm UTC](https://www.ruby-forum.com/t/changing-sidebar-in-layout-per-controller/86741/3 "2006-03-24T14:04:51Z")

</div>

Charlie B. wrote:

> I accomplished this exact same task by creating a method in my  
> application helper. This method would dynamically create my navbar  
> based on a context variable that I would set in each template. (\<%  
> @context = ‘about’ %\>). I would call my application helper method in my  
> layout and let it return my modified navbar. This solution works great  
> for me.
> 
> On Fri, 2006-03-24 at 12:20 +0100, James W. wrote:
> 
> > What is the best actionview way to do this. I don’t want loads of  
> > differing layouts as there could be many different controllers in  
> > future.
> > 
> > I could use render partial but then in each of the controllers actions I  
> > would have a load of repeated html breaking the DRY rules.
> 
> Charlie B.  
> [http://www.recentrambles.com](http://www.recentrambles.com)

Thanks, I have just started doing just that after sitting down, having a  
cup of coffee and thinking about it. 🙂

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [March 24, 2006, 3:02pm UTC](https://www.ruby-forum.com/t/changing-sidebar-in-layout-per-controller/86741/4 "2006-03-24T15:02:27Z")

</div>

I have a sidebar method in application.rb that renders the default  
sidebar.  
I can the override this method is whatever controllers that might need  
it

In the layout i do a render\_component calling sidebar on the current  
controller.

I also pass the original action and the original id, these may be handy  
when rendering some specific sidebar

On Friday, March 24, 2006, at 2:04 PM, James W. wrote:

> > 
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Mikkel B.

www.strongside.dk - Football Portal(DK)  
[nflfeed.helenius.org](http://nflfeed.helenius.org) - Football News(DK)  
ting.minline.dk - Buy Old Stuff!(DK)
