【发布时间】:2012-02-06 00:22:27
【问题描述】:
我正在制作一个电子商务应用程序,其中类别在所有页面的侧栏中都可见。我在应用程序控制器中写了一个方法
def categories
@categories = Category.all
end
但是我怎样才能让这个方法在默认情况下对所有控制器和动作都可用,这样我就不必在每个动作中专门调用这个方法
def list
categories
@products = Product.order('title').page(params[:page]).per(4)
end
【问题讨论】:
-
在 application_controller 中定义该方法并在应用控制器定义后添加
helper_method :categories(你想要的称为helper_method)
标签: ruby-on-rails global before-filter view-helpers