开发者

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:

  1. put the method inside a module or applications_controller.rb
  2. add before_filter in applications_controller to populate the instance variable
  3. add an extra partial app/viewes/shared/_latest_announcement.hmtl.erb
  4. include the partial into the main layout applications.html.erb
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜