# Rendering partials based on conditions

**URL:** <https://www.ruby-forum.com/t/rendering-partials-based-on-conditions/101873>\
**Category:** Rails\
**Created:** [August 8, 2006, 1:41pm UTC](https://www.ruby-forum.com/t/rendering-partials-based-on-conditions/101873 "2006-08-08T13:41:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [August 8, 2006, 1:41pm UTC](https://www.ruby-forum.com/t/rendering-partials-based-on-conditions/101873/1 "2006-08-08T13:41:31Z")

</div>

Hi,  
A bit unsure how this gets accomplished - particularly how it might  
get set up in the layout.  
Currently -  
This is my welcome.rthml (what comes up at main page)  
\<% if @u %\>  
\<%= render :partial =\> “main/session” %\>  
\<% else %\>  
\<%= render :partial =\> “main/login” %\>  
\<%= render :partial =\> “main/register” %\>  
\<% end %\>

But in my layout currently to make sure in what divs partials are  
placed I’m doing this:

\<%= render(:partial =\> "session" ) %\>

\<%= render(:partial =\> "register" ) %\> \<%= render(:partial =\> "login" ) %\>

Because the layout is set up as it is, the if /else in the welcome  
doesn’t really effect output.  
Any suggestions ?

TIA  
Stuart

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [August 8, 2006, 10:09pm UTC](https://www.ruby-forum.com/t/rendering-partials-based-on-conditions/101873/2 "2006-08-08T22:09:35Z")

</div>

On Aug 8, 2006, at 4:41 AM, Dark A. wrote:

> Because the layout is set up as it is, the if /else in the welcome  
> doesn’t really effect output.

I’m not really clear what this problem is. It appears you’re  
rendering the same partial twice, once in the layout and once  
in the view, but I may be entirely misunderstanding.

> Any suggestions ?

How about putting the conditional renders into a helper?

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [August 9, 2006, 4:36am UTC](https://www.ruby-forum.com/t/rendering-partials-based-on-conditions/101873/3 "2006-08-09T04:36:55Z")

</div>

Errr…

\<%= render(:partial =\> "session" ) if @u %\>

\<% unless @u %\> \<%= render(:partial =\> "register" ) %\> \<%= render(:partial =\> "login" ) %\> \<% end %\>

Max

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [August 9, 2006, 7:40am UTC](https://www.ruby-forum.com/t/rendering-partials-based-on-conditions/101873/4 "2006-08-09T07:40:06Z")

</div>

Stuart Fellowes wrote:

> Hi,  
> A bit unsure how this gets accomplished - particularly how it might  
> get set up in the layout.  
> Currently -  
> This is my welcome.rthml (what comes up at main page)  
> \<% if @u %\>  
> \<%= render :partial =\> “main/session” %\>  
> \<% else %\>  
> \<%= render :partial =\> “main/login” %\>  
> \<%= render :partial =\> “main/register” %\>  
> \<% end %\>
> 
> But in my layout currently to make sure in what divs partials are  
> placed I’m doing this:
> 
> \<%= render(:partial =\> "session" ) %\>
> 
> \<%= render(:partial =\> "register" ) %\> \<%= render(:partial =\> "login" ) %\>
> 
> Because the layout is set up as it is, the if /else in the welcome  
> doesn’t really effect output.  
> Any suggestions ?
> 
> TIA  
> Stuart

In your layout, you should add @content\_for\_layout. This is where  
welcome.rhtml be placed.
