我想知道是否有任何方法可以在应用程序控制器中为索引和显示操作定义默认响应,并能够在需要一些自定义的其他控制器中覆盖。
我认为举个例子会更容易。
我正在使用 InheritedResources、CanCan/Authlogic 和 WickedPDF gem 来生成我的 pdf 文件并授权用户。我想知道我是否可以干掉我的代码。
这是我所拥有的
class ProductsController < InheritedResources::Base
load_and_authorize_resource
respond_to :html, :xml, :json, :pdf
def index
@products = Product.page(params[:page])
index! do |format|
format.pdf do
render :pdf => pdf_file_name,
:show_as_html => params[:debug].present?
end
end
end
def show
show! do |format|
format.pdf do
render :pdf => pdf_file_name,
:show_as_html => params[:debug].present?
end
end
end
end
class CustomersController < InheritedResources::Base
def index
index! do |format|
format.pdf do
render :pdf => pdf_file_name,
:show_as_html => params[:debug].present?
end
end
end
def show
show! do |format|
format.pdf do
render :pdf => pdf_file_name,
:show_as_html => params[:debug].present?
end
end
end
end
这工作得很好。但是,我需要在要为其生成 pdf 的每个控制器中定义 format.pdf 似乎是多余的。有没有办法将它移动到应用程序控制器或使用继承的资源指定某处,然后在每个控制器的基础上覆盖它?有任何想法吗?
谢谢