2012-03-12 4 views
2

일부 사용자 지정이 필요한 다른 컨트롤러에서 재정의 할 수있는 기능으로 응용 프로그램 컨트롤러에서 인덱스 & 동작을 표시하기 위해 기본 respond_to를 정의하는 방법이 있는지 궁금합니다.응용 프로그램 컨트롤러에서 색인 및 표시 작업에 대한 기본 respond_to를 정의하려면 어떻게합니까?

예를 들어 더 쉽게 생각할 수 있습니다.

내 PDF를 생성하고 사용자를 인증하기 위해 InheritedResources, CanCan/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를 정의해야 할 필요가 없어 보인다. 이것을 응용 프로그램 컨트롤러로 옮기거나 상속 된 리소스를 사용하여 어딘가에 지정하는 방법이 있습니까? 그리고 컨트롤러별로이 옵션을 무시하십시오. 어떤 아이디어?

내가 다른 관심있는 사람들을위한 다음과 같은 솔루션을 함께했다 당신이

답변

2

확인 감사드립니다.

ApplicationController에서 상속 한 InheritedResources를 상속 한 컨트롤러를 추가 한 다음 다른 모든 컨트롤러를 상속받을 수 있다고 생각했습니다. (애플리케이션 컨트롤러에서 직접 상속받을 특별한 경우는 제외하고 (예 : HomeController, 인덱스 이외의 다른 동작이없고 특정 모델에 묶여 있지 않음) - 특정 기본을 정의 할 수있는이 방법 - 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 

희망이 있으면 도움이 될 것입니다.