hi all, Im looking for some pointers on best practices of when to use a helper and when to use a partial. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
first, helpers... for instance, i need to be able to display a sentence such as "There are X things in your queue" where X is an integer now, depending on the number of things, the grammar will be different. if there is only 1 thing, I need it to say "There is 1 thing...", with 2 or more, it needs to read "There are 2 things..." and with 0 things, "There is nothing..." now i could put all the code to handle this in my template, but that would be ugly (and not DRY), or i could but in a helper and just call the helper when I need to display that sentence correctly def queue_title(things = 0) title = "There " case things when 0 title += "is nothing" when 1 title += "is 1 thing" else title += "are #{things} things" end title += " in your queue" title end then in my view template <%= title_for_queue(@things.size) -> now a partial is a chunk of your view template that you want to either reuse or use repeatedly. candiates for partials are table rows, list items, forms, etc. so say i have a form that i want to use a few different views, i would put it into a partial _my_form.rhtml <form ...> ... </form> and wherever i want to show this form i can call <%= render :partial => "my_form" -%> i suggest you also read the rails docs on helpers and partials. hope this helps Chris On 1/20/07, weepy <jonahfox-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > hi all, > > Im looking for some pointers on best practices of when to use a helper > and when to use a partial. > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
augustlilleaas-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jan-20 18:58 UTC
Re: Helper versus Partial
My rule of thumb is that if the rhtml is way to full of ERb <% %> tags, I''ll make a helper instead. On Jan 20, 6:35 pm, "Chris Hall" <christopher.k.h...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> first, helpers... > > for instance, i need to be able to display a sentence such as > > "There are X things in your queue" > > where X is an integer > > now, depending on the number of things, the grammar will be different. > if there is only 1 thing, I need it to say "There is 1 thing...", > with 2 or more, it needs to read "There are 2 things..." and with 0 > things, "There is nothing..." > > now i could put all the code to handle this in my template, but that > would be ugly (and not DRY), or i could but in a helper and just call > the helper when I need to display that sentence correctly > > def queue_title(things = 0) > title = "There " > case things > when 0 > title += "is nothing" > when 1 > title += "is 1 thing" > else > title += "are #{things} things" > end > title += " in your queue" > title > end > > then in my view template > > <%= title_for_queue(@things.size) -> > > now a partial is a chunk of your view template that you want to either > reuse or use repeatedly. candiates for partials are table rows, list > items, forms, etc. > > so say i have a form that i want to use a few different views, i would > put it into a partial > > _my_form.rhtml > > <form ...> > ... > </form> > > and wherever i want to show this form i can call > > <%= render :partial => "my_form" -%> > > i suggest you also read the rails docs on helpers and partials. > > hope this helps > > Chris > On 1/20/07, weepy <jonah...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > hi all, > > > Im looking for some pointers on best practices of when to use a helper > > and when to use a partial.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---