2012-03-12 5 views
2

は、いくつかのカスタマイズを必要とする他のコントローラに上書きする機能をアプリケーションコントローラのインデックス&ショーのアクションのデフォルトのrespond_toを定義するためにどのような方法があります。アプリケーションコントローラのインデックス&ショーアクションにデフォルトのrespond_toを定義するにはどうすればよいですか?私が思っていた

私はそれを例に簡単になるだろうと思います。

私は私のPDFファイルを生成し、ユーザを認証するためにInheritedResources、カンカン/ AuthlogicとWickedPDFの宝石を使用しています。私は私のコードを枯渇させることができるかどうか疑問に思っていた。ここで

は、私はこれだけで正常に動作します

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を定義する必要があることは冗長に思えます。これをアプリケーションコントローラに移動する方法や、継承されたリソースを使用してどこかを指定する方法はありますか?コントローラごとにこれをオーバーライドしてください。何か案は?

は、私が興味を持って他の誰のために、次の解決策を考え出したあなたに[OK]を

答えて

2

ありがとうございます。

私は、ApplicationControllerから継承したInheritedResourcesを継承するコントローラを追加してから、他のすべてのコントローラを継承するように考えました(アプリケーションコントローラから直接継承する特別なケースを除いてHomeController、index以外のアクションはなく、特定のモデルには束縛されていません) - 特定のデフォルトを定義することができます - 私はrespond_toのようなすべてのコントローラで使用し続けますが、InheritedResources宝石。

class DefaultInheritedResourcesController < InheritedResources::Base 
    # For CanCan authorization - pretty much makes it application wide, without the need 
    # to define it in each controller. Can still override it in ability.rb by making  
    # a resource readable to all users with a session. 
    # if user 
    # can :read, [Product] 
    # end 
    # Also for controllers that need special treatment, you can just inherit from ApplicationController 
    # and override with skip_authorization_check - but for my app it's rare (only HomeController), 
    # most of controllers deal with some kind of resource - so this is a useful convention for 99% of use cases. 

    load_and_authorize_resource 
    respond_to :html, :json, :xml, :pdf 

    # format pdf needs to be redefined on those actions where index! or show! are called. 
    def index 
    super do |format| 
     format.pdf do 
     render :pdf => pdf_file_name, 
       :show_as_html => params[:debug].present? 
     end 
    end 
    end 

    def show 
    super do |format| 
     format.pdf do 
     render :pdf => pdf_file_name, 
       :show_as_html => params[:debug].present? 
     end 
    end 
    end 
end 

その後、私のProductControllerに私は私のProductControllerから継承されたこの(通知を行うことができます。

class ProductsController < DefaultInheritedResourcesController 
    def index 
    @products = Product.page(params[:page]) 
    super 
    end 
end 

これは誰かを助けるつもりです。

関連する問題