# Render an alternate partial

**URL:** <https://www.ruby-forum.com/t/render-an-alternate-partial/157747>\
**Category:** Rails\
**Created:** [January 14, 2009, 11:39pm UTC](https://www.ruby-forum.com/t/render-an-alternate-partial/157747 "2009-01-14T23:39:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnsonmlw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f05b48/32.png) [@johnsonmlw](https://www.ruby-forum.com/u/johnsonmlw)\
**Post date:** [January 14, 2009, 11:39pm UTC](https://www.ruby-forum.com/t/render-an-alternate-partial/157747/1 "2009-01-14T23:39:48Z")

</div>

I want to render a partial from two different views (class/index;  
cohort/index) in order to iterate (?) through the pupils model using  
\_pupil.html.erb (in the pupils/views directory)

class has many pupils

cohort has many pupils

pupil belongs to class  
pupil belongs to cohort

It works well, and the \_pupil.html.erb partial is rendered correctly  
from both views.

- But, and this is the thing, I want the pupil partial to be slightly  
different in each case i.e. when it’s called from cohort/index I want  
to display the pupil data in a table, but not when it’s rendered from  
class/index.

How do I go about having two \_pupil.html.erb partials without breaking  
links between views/controllers/models please?

## Thanks

Matt

---

<div class="post-metadata">

**Author:** ![johnsonmlw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f05b48/32.png) [@johnsonmlw](https://www.ruby-forum.com/u/johnsonmlw)\
**Post date:** [January 15, 2009, 12:13am UTC](https://www.ruby-forum.com/t/render-an-alternate-partial/157747/2 "2009-01-15T00:13:51Z")

</div>

> It works well, and the \_pupil.html.erb partial is rendered correctly  
> from both views.
> 
> - But, and this is the thing, I want the pupil partial to be slightly  
> different in each case i.e. when it’s called from cohort/index I want  
> to display the pupil data in a table, but not when it’s rendered from  
> class/index.
> 
> How do I go about having two \_pupil.html.erb partials without breaking  
> links between views/controllers/models please?

Why wouldn’t you just move views/pupils/\_pupil.html.erb into views/  
class/\_pupil.html.erb and views/cohort/\_pupil.html.erb and change them  
to do what you want? Then update the render :partial calls from the  
two calling files to look to their own version?

---

<div class="post-metadata">

**Author:** ![johnsonmlw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f05b48/32.png) [@johnsonmlw](https://www.ruby-forum.com/u/johnsonmlw)\
**Post date:** [January 15, 2009, 4:15pm UTC](https://www.ruby-forum.com/t/render-an-alternate-partial/157747/3 "2009-01-15T16:15:14Z")

</div>

That’s exactly it. Thank you!

I’ll do it.

What does that bit of rendering code look like. How do I keep the  
@cohort.pupils bit but tell it to go to the different directory?

At the moment it looks like this and points to the pupils directory:

 \<%= render :partial =\> @cohort.pupils %\>

I’m aware this should be obvious but I don’t understand the API docs  
on this.

–  
Matt

---

<div class="post-metadata">

**Author:** ![johnsonmlw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f05b48/32.png) [@johnsonmlw](https://www.ruby-forum.com/u/johnsonmlw)\
**Post date:** [January 15, 2009, 7:32pm UTC](https://www.ruby-forum.com/t/render-an-alternate-partial/157747/4 "2009-01-15T19:32:38Z")

</div>

Sorted it with :collection

Thanks for the tip.
