Hi, I have a site which basically has a layout page displaying the 3 latest news items down the left hand side of the page and the main content in the center. The layout renders a partial to display the 3 latest news items _latestnews.rhtml <% for article in @latestarticles %> <a href=""><%= article.title %> </a><small>(<%= article.created_at.to_s(:long) %>)</small> <br/> <br/> <% end %> <%= link_to ''Read more recent news items'', :action => ''news''%>» layout.rhtml The layout also contains a reference to the main content <%= @content_for_layout %>. controller def home @latestarticles = Article.get_latest_news() end When i click on the about_us page i get an error because the new page doesnt have access to the 3 latest news items so for the about us page to get it to work i have to the following def about_us @latestarticles = Article.get_latest_news() end and for the contact page def contact @latestarticles = Article.get_latest_news() end So for every page in the site i want the 3 latest news items displayed down the left hand side but i dont want to have to include @latestarticles = Article.get_latest_news() in every method when i show a new page. Whats the best way to manage this as im not really experienced with caching, sessions and stuff like that. Appreciate any advice? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> I have a site which basically has a layout page displaying the 3 latest > news items down the left hand side of the page and the main content in > the center. The layout renders a partial to display the 3 latest news > items > > _latestnews.rhtml > <% for article in @latestarticles %> > <a href=""><%= article.title %> </a><small>(<%> article.created_at.to_s(:long) %>)</small> > <br/> > <br/> > <% end %> > <%= link_to ''Read more recent news items'', :action => ''news''%>» > > layout.rhtml > The layout also contains a reference to the main content <%> @content_for_layout %>. > > controller > def home > @latestarticles = Article.get_latest_news() > end > > When i click on the about_us page i get an error because the new page > doesnt have access to the 3 latest news items so for the about us page > to get it to work i have to the following > > def about_us > @latestarticles = Article.get_latest_news() > end > > and for the contact page > def contact > @latestarticles = Article.get_latest_news() > end > > So for every page in the site i want the 3 latest news items displayed > down the left hand side but i dont want to have to include > @latestarticles = Article.get_latest_news() in every method when i show > a new page. > > Whats the best way to manage this as im not really experienced with > caching, sessions and stuff like that. > > Appreciate any advice?In your app/controllers/application.rb file add the following: ----------------------------------------------------------- before_filter :get_latest_articles def get_latest_articles @latestarticles = Article.get_latest_news() end ----------------------------------------------------------- Now *every* controller/action will have access to @latestarticles and you can remove it from those actions themselves. This won''t cache anything, but it will DRY things up for you... -philip --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Philip Hallstrom wrote:>> <br/> >> end >> def contact >> >> Appreciate any advice? > > In your app/controllers/application.rb file add the following: > > ----------------------------------------------------------- > before_filter :get_latest_articles > > def get_latest_articles > @latestarticles = Article.get_latest_news() > end > ----------------------------------------------------------- > > Now *every* controller/action will have access to @latestarticles and > you > can remove it from those actions themselves. > > This won''t cache anything, but it will DRY things up for you... > > -philipThanks Philip -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---