how to use a controller variable in my layout
I am trying to use a variable开发者_JS百科 of my controller in my layout.
For example:
@posts = Post.all.count
In my layout I want to list the Post count, even when I open the index view of another controller.
Many thanks!!!
Two solutions:
- Use
<%= Post.all.count %>
in your layout. Add a
before_filter
in yourApplicationController
that loads the variable.class ApplicationController < ActionController::Base before_filter :load_layout_variables protected def load_layout_variables @posts = Post.all.count end end
精彩评论