Layout 如何在我的布局中使用控制器变量

Layout 如何在我的布局中使用控制器变量,layout,controller,ruby-on-rails-3,Layout,Controller,Ruby On Rails 3,我试图在布局中使用控制器的变量 例如: @posts=Post.all.count 在我的布局中,我想列出Post计数,即使在打开另一个控制器的索引视图时也是如此 非常感谢 两种解决方案: 在布局中使用 在加载变量的应用程序控制器中的过滤器之前添加一个 class ApplicationController < ActionController::Base before_filter :load_layout_variables protected def load_layou

我试图在布局中使用控制器的变量

例如:
@posts=Post.all.count

在我的布局中,我想列出Post计数,即使在打开另一个控制器的索引视图时也是如此

非常感谢

两种解决方案:

  • 在布局中使用
  • 在加载变量的
    应用程序控制器
    中的过滤器之前添加一个

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