I have this class Pic < ActiveRecord::Base def self.active_pics find(:all, :conditions => ["thumbnail is NULL AND active = 1"]) end end in my pic.rb model file. I have an area on my site which uses a different layout file (pic.html.erb). Within the layout file I render a special partial (_pics_nav.html.erb): <% for pic in @pics %> <%= pic.description %> <% end %> So I get a nil object. Where tu put this code so I have access within every layout file and every partial? Thanx -- Jochen Kaechelin --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2007-Oct-26 11:00 UTC
Re: Where to put this to have access from everywhere
On 26 Oct 2007, at 11:56, Jochen Kaechelin wrote:> > I have this > > class Pic < ActiveRecord::Base > def self.active_pics > find(:all, :conditions => ["thumbnail is NULL AND active = 1"]) > end > end > > in my pic.rb model file. > > I have an area on my site which uses a different layout file > (pic.html.erb). Within the layout file I render a special partial > (_pics_nav.html.erb): > > <% for pic in @pics %> > <%= pic.description %> > <% end %> > > So I get a nil object. > > Where tu put this code so I have access within every layout file and > every partial? >Don''t rely on the instance variable : <%= render :partial => ''/shared/pics_nav'', :object => Pic.active_pics %> If this is quite common, i''d stick something in application helper def render_pic_nav_bar render :partial => ''/shared/pics_nav'', :object => Pic.active_pics %> end and then you just do <%= render_pic_nav_bar%> when you need it. Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jochen Kaechelin
2007-Oct-26 11:06 UTC
Re: Where to put this to have access from everywhere
Frederick Cheung schrieb:> > On 26 Oct 2007, at 11:56, Jochen Kaechelin wrote: > >> I have this >> >> class Pic < ActiveRecord::Base >> def self.active_pics >> find(:all, :conditions => ["thumbnail is NULL AND active = 1"]) >> end >> end >> >> in my pic.rb model file. >> >> I have an area on my site which uses a different layout file >> (pic.html.erb). Within the layout file I render a special partial >> (_pics_nav.html.erb): >> >> <% for pic in @pics %> >> <%= pic.description %> >> <% end %> >> >> So I get a nil object. >> >> Where tu put this code so I have access within every layout file and >> every partial? >> > > Don''t rely on the instance variable : > <%= render :partial => ''/shared/pics_nav'', :object => Pic.active_pics %> > > If this is quite common, i''d stick something in application helper > > def render_pic_nav_bar > render :partial => ''/shared/pics_nav'', :object => Pic.active_pics %> > end > > and then you just do <%= render_pic_nav_bar%> when you need it. > FredWow! That easy! Thanx a lot!! -- Jochen Kaechelin --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---