开发者

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 your ApplicationController that loads the variable.

    class ApplicationController < ActionController::Base
      before_filter :load_layout_variables
    
    protected
      def load_layout_variables
        @posts = Post.all.count
      end
    end
    
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜