Application Controller で index および show アクションのデフォルトの Respond_to を定義し、カスタマイズが必要な他のコントローラーでオーバーライドできるようにする方法はあるのでしょうか。
例があるとわかりやすいと思います。
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 を定義する必要があるのは冗長に思えます。これをアプリケーションコントローラーに移動するか、継承されたリソースを使用してどこかを指定し、コントローラーごとにこれをオーバーライドする方法はありますか? 何か案は?
ありがとうございました