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? -%>
<p>There are currently no testimonials.</p>
<% else -%>
<blockquote>
<p><%= testimonial.comment %></p>
<cite><%= testimonial.cite -%><% if not testimonial.company.blank?
%>, <%= testimonial.company -%><% end -%></cite>
</blockquote>
<% 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 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? -%>
<p>There are currently no testimonials.</p>
<% else -%>
<blockquote>
<p><%= testimonial.comment %></p>
<cite><%= testimonial.cite -%><% if not testimonial.company.blank?
%>, <%= testimonial.company -%><% end -%></cite>
</blockquote>
<% end -%>
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.