Caching/session Question

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 %>
<%= article.title %> (<%=
article.created_at.to_s(:long) %>)





<% end %>
<%= link_to ‘Read more recent news items’, :action => ‘news’%>&#187

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?


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…

-philip

Philip H. wrote:


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…

-philip

Thanks Philip

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs