我想知道是否有任何方法來定義索引&索引&的默認respond_to顯示在應用程序控制器中的操作,並具有在需要一些定製的其他控制器中重寫的能力。如何爲應用程序控制器中的index&show動作定義default respond_to?
我認爲用一個例子會更容易。
我使用了InheritedResources,CanCan/Authlogic和WickedPDF gems來生成我的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似乎是多餘的。有什麼辦法可以將它移動到應用程序控制器或指定使用繼承資源的地方,然後在每個控制器的基礎上覆蓋它?有任何想法嗎?
謝謝