How to display results globally, rails3
We wanted to include a global announcements module in our rails3 application that's displayed on all pages when a user's logged in to the system.
I've created an announcement controller and, to test, put the following in to display the last active message:
@latest_announcement = Announcement.where(:active => true).order("created_at").last
What I was trying to do was display the result of @latest_announcement on every page (in my application layout) and I tried to put this in my application controller.
However, this didn't really work out for me.
Is there a simple way to do thi开发者_运维技巧s without having to put the above in every controller?
if you have shared notification across the system and need to put it on the every page, you can do:
- put the method inside a module or applications_controller.rb
- add before_filter in applications_controller to populate the instance variable
- add an extra partial app/viewes/shared/_latest_announcement.hmtl.erb
- include the partial into the main layout applications.html.erb
精彩评论