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 a lot everyone,
Blaine
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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 %>
-----Original Message-----
From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
[mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of
blaine
Sent: Sunday, March 11, 2007 11:32 PM
To: Ruby on Rails: Talk
Subject: [Rails] Partials and making it DRY
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 a lot everyone,
Blaine
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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 -%> On Mar 12, 12:08 am, "Meech" <meech...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 %> > > -----Original Message----- > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > > [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of blaine > Sent: Sunday, March 11, 2007 11:32 PM > To: Ruby on Rails: Talk > Subject: [Rails] Partials and making it DRY > > 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 a lot everyone, > > Blaine--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Mar 12, 12:31 am, "blaine" <jangc...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 -%> > > On Mar 12, 12:08 am, "Meech" <meech...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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 %> > > > -----Original Message----- > > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > > > [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of blaine > > Sent: Sunday, March 11, 2007 11:32 PM > > To: Ruby on Rails: Talk > > Subject: [Rails] Partials and making it DRY > > > 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 a lot everyone, > > > Blaine--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---