# Partials and making it DRY

**URL:** <https://www.ruby-forum.com/t/partials-and-making-it-dry/88501>\
**Category:** Rails\
**Created:** [March 12, 2007, 4:32am UTC](https://www.ruby-forum.com/t/partials-and-making-it-dry/88501 "2007-03-12T04:32:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![blaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/51bf81/32.png) [@blaine](https://www.ruby-forum.com/u/blaine)\
**Post date:** [March 12, 2007, 4:32am UTC](https://www.ruby-forum.com/t/partials-and-making-it-dry/88501/1 "2007-03-12T04:32:30Z")

</div>

I’m trying to take advantage of partials to display a random  
testimonial a few pages and I think my newness is getting in the way  
here, I’m hoping someone can offer some assistance.

In my controller I have:

def index  
@title = “Testimonials”  
@testimonials = Testimonial.find(:all)  
end

def random  
@testimonials = Testimonial.find(:first, :order =\> ‘RAND()’)  
end

Now I have a partial for random, but how would I get it to call the  
random method? The display is similar to the all display of (without  
the loop):

\<% if @testimonials.blank? -%\>

There are currently no testimonials.

\<% else -%\> 

> \<%= testimonial.comment %\>
> 
> \<%= testimonial.cite -%\>\<% if not testimonial.company.blank? %\>, \<%= testimonial.company -%\>\<% end -%\>

\<% end -%\>

Lastly as well, being that the full list and the random code is so  
similar minus the loop - is there a way I can make this a little more  
DRY by having it in the same partial somehow?

Thanks a lot everyone,

Blaine

---

<div class="post-metadata">

**Author:** ![blaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/51bf81/32.png) [@blaine](https://www.ruby-forum.com/u/blaine)\
**Post date:** [March 12, 2007, 5:13am UTC](https://www.ruby-forum.com/t/partials-and-making-it-dry/88501/2 "2007-03-12T05:13:33Z")

</div>

You can wrap it in a helper in application\_helper

def show\_random\_testimonials  
@testimonials = Testimonial.find(:first, :order =\> ‘RAND()’)  
render :partial =\> ‘shared/testimonials’  
end

Then in your rhtml

\<%= show\_random\_testimonials %\>

---

<div class="post-metadata">

**Author:** ![blaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/51bf81/32.png) [@blaine](https://www.ruby-forum.com/u/blaine)\
**Post date:** [March 12, 2007, 5:32am UTC](https://www.ruby-forum.com/t/partials-and-making-it-dry/88501/3 "2007-03-12T05:32:35Z")

</div>

Thanks Meech - but what should the output partial rhtml look like? -  
I’m still getting a “undefined local variable or method `testimonial’”  
on what I have:

\<% if @testimonials.blank? -%\>

There are currently no testimonials.

\<% else -%\> 

> \<%= testimonial.comment %\>
> 
> \<%= testimonial.cite -%\>\<% if not testimonial.company.blank? %\>, \<%= testimonial.company -%\>\<% end -%\>

\<% end -%\>

---

<div class="post-metadata">

**Author:** ![blaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/51bf81/32.png) [@blaine](https://www.ruby-forum.com/u/blaine)\
**Post date:** [March 12, 2007, 6:34pm UTC](https://www.ruby-forum.com/t/partials-and-making-it-dry/88501/4 "2007-03-12T18:34:14Z")

</div>

Ok I’ve got it… I was confusing the testimonial.comment from the  
loop because I was doing a “for testimonial in @testimonials” so I was  
referencing the incorrect method in the view. Simple neophyte  
mistake.

Thanks,

Tim
