I’m setting up a forum and I’m having a sidebar which will display the
recent postings and will be used throughout the site. This sidebar is
part of the application.rhtml layout.
How do I use data from the “forum” controller in this layout when the
layout is used on all of the other controllers across the site?
Could someone point me in the right direction?
Many thanks - when I get a little more advanced I hope I can contribute
as much advice as you guys have so kindly given me 
I’d say the nicest/cleanest thing to do would be to set that sidebar to
be
populated via AJAX. When the page loads, send of a request to the forum
controller and update the sidebar div.
Jason
Jason R. wrote:
I’d say the nicest/cleanest thing to do would be to set that sidebar to
be
populated via AJAX. When the page loads, send of a request to the forum
controller and update the sidebar div.
Jason
Thanks but AJAX is something on my list to learn after I’ve got ror
under my belt! 
Is there any other way to do it with application_help, or by rendering
partials etc?
Thanks
Well, here’s all the code you would need to get the AJAX working:
application.rhtml:
…
<%= javascript_tag remote_function(:update => 'sidebar', :url =>
{:controller => 'forum_controller', :action => 'sidebar_action' }) %>
I’m assuming the action on the forum controller already exists, and
you’d
just need a partial that renders just those items.
However, if you really want a non-ajax format, I guess you would need a
method on the Forum model (if you don’t already have that) and then in
your
application.rb you can have:
class Application …
after_filter :fill_in_sidebar
def fill_in_sidebar
@sidebar_posts = Forum.find_newest_posts
end
end
This will call #fill_in_sidebar after any action on any controller.
Hope that helps.
Jason
Scott H. wrote:
as much advice as you guys have so kindly given me 
Use a partial (perhaps in a /shared folder) and populate it with an
instance variable set up in the application controller using a
before_filter.
So something like this
_recent_postings_sidebar.rhtml
<% @recent_posts.each do |post| %>
<%= post.title %>
<% end %>
in the application controller
:before_filter :setup_sidebar
def setup_sidebar
@recent_posts = Post.find(:all, :limit => 20, :order =>
“created_at DESC”)
end
and then in your layout:
render :partial => “/shared/recent_postings_sidebar”
So something like this
_recent_postings_sidebar.rhtml
<% @recent_posts.each do |post| %>
<%= post.title %>
<% end %>
in the application controller
:before_filter :setup_sidebar
def setup_sidebar
@recent_posts = Post.find(:all, :limit => 20, :order =>
“created_at DESC”)
end
and then in your layout:
render :partial => “/shared/recent_postings_sidebar”
This works great on my local machine, (using webrick) but when I use it
on an apache fcgi server (dreamhost) the variable does not update after
it has been initially set.
Anyone have any ideas how to fix this?
On Dec 18, 6:37 pm, Chris T [email protected] wrote:
Many thanks - when I get a little more advanced I hope I can contribute
<% end %>
render :partial => “/shared/recent_postings_sidebar”
That worked great on my local machine, but when I uploaded it to a
server using fcgi the variable does not update once it has been
initially set.
Any ideas?