# Right way to pass conditionals to partials

**URL:** <https://www.ruby-forum.com/t/right-way-to-pass-conditionals-to-partials/57862>\
**Category:** Rails\
**Created:** [April 15, 2006, 5:20am UTC](https://www.ruby-forum.com/t/right-way-to-pass-conditionals-to-partials/57862 "2006-04-15T05:20:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jowanp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a183cd/32.png) [@jowanp](https://www.ruby-forum.com/u/jowanp)\
**Post date:** [April 15, 2006, 5:20am UTC](https://www.ruby-forum.com/t/right-way-to-pass-conditionals-to-partials/57862/1 "2006-04-15T05:20:31Z")

</div>

Hi All,

I have some partials that need to be rendered a bit differently  
depending on the context. To make matters worse, sometimes these  
partials are including other partials, that I’d like to control from the  
main template. These partials are templates that look similar in  
different contexts, but have different controls depending on where  
they’re being rendered. I tried locals, but the result looks ugly - I  
basically have to do this:

\<% if !defined?(nocontrols) && !nocontrols -\>  
html to render controls  
\<% end -\>

Then in my main template I call \<% render(:partial =\> “partial”, :locals  
=\> { :nocontrols =\> true }) \>

This works, but the convoluted if statements make me cringe. Plus, I’d  
have to specifically pass these locals to any nested partials, and that  
gets too complicated.

Declaring locals in controllers work, but then I can’t include two  
partials with different parameters…

There has to be a better way to do this.

---

<div class="post-metadata">

**Author:** ![jowanp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a183cd/32.png) [@jowanp](https://www.ruby-forum.com/u/jowanp)\
**Post date:** [April 15, 2006, 10:04pm UTC](https://www.ruby-forum.com/t/right-way-to-pass-conditionals-to-partials/57862/2 "2006-04-15T22:04:44Z")

</div>

> There has to be a better way to do this.

What abt a Helper() ? If nothing else you could consolidate the  
convoluted IF statements… Looks like youre tyring to do a role based  
exposure of controls , e.g. an admin can see this button, etc. If so it  
might be worth considering some kind of container that would have the  
html controls [do don’t this in the model tho - bad violation of MVC]  
for a given roll - then the view [your .rhtml] can simply render whats  
in the container [probably just a plain old ruby object] - the  
controller would of course populate the container based on the role of  
the current user. This way your view simply renders whats there [which  
could mean nothing, if applicable]… this would alleviate the need for  
any role-type processing in your partials, i.e. avoid the problem all  
together.
