2014-10-02 3 views
0

필요한 것은 레일에서 자동 페이지 (HTML) 렌더링을 비활성화하고 after_action 메서드로 재정의하는 것입니다. 달성하고자하는 것은 CakePHP $ this-> autoRender = false와 동등합니다.레일에서 자동 렌더링 사용 안 함

application_controller.rb

class ApplicationController < ActionController::Base 

    after_action :custom_render 

    layout nil # Tried this but didn't worked 

    def custom_render 
     render #[...] 
    end 


end 

some_controller.rb

class SomeController < ApplicationController 

    def index 
     # No rendering here 
    end 

end 
내가 렌더링에서 모든 작업을 방지하기 위해 레이아웃 전무를 추가하려고 코드에 나타낸 바와 같이

하지만이하지 않는 것 행동의 행동에 영향을 미친다.

+2

실제로 'after_action'이 무엇을위한 것인지는 아닙니다. 요점은 렌더링 된 응답에 액세스 할 수 있다는 것입니다. 사용자 정의 렌더링 방법을 작성하려면 [더 좋은 방법]이 있습니다 (http://beerlington.com/blog/2011/07/25/building-a-csv-renderer-in-rails-3/). 실제 문제에 대해 더 많이 이야기 할 수 있다면'$ this-> autoRender = false'와 동등한 해결책이 될 수 있습니다. – meagar

+0

테마 스위처를 만들려고하는데, ApplicationController는 사용자가 선택한 테마를 데이터베이스에서 선택하고 적절한 템플릿을 렌더링합니다. – bukk530

+0

당신이'ThemesController'의'random' 액션에 연결하고 싶다면 ...? ApplicationController가 모든 렌더링을 수행하면 액션 내부에서 무엇을 할 수 있습니까? – meagar

답변

0

렌더링을 사용하지 않으려면 (아무 것도 반환하지 않음) 문제가 있습니다.

def index 
    render :nothing 
end 

빈 몸체에 대한 응답을 반환하기 때문에 너무 늦었습니다.

def index 
    render layout: false 
end 

이것은 당신이 레이아웃없이 볼 렌더링, 문제 (render layout: 'my_custom_layout'가) 기본보기하지만 서로 다른 레이아웃을 렌더링 :

는 레이아웃을 사용하지 않으려면.

def index 
    render 'my_custom_file.' 
end 

정말 많은 옵션이 있습니다 :

우리는 당신이 원하는,하지만 간단한 솔루션은 단지 특정보기 Fi를 렌더링하는 것입니다 모르는 http://guides.rubyonrails.org/layouts_and_rendering.html#using-render

편집를 -로 의견에서 요청했습니다.

class ApplicationController < ActionController::Base 
    before_action :set_user_template 

    # ... 

    def set_user_template 
    template_name = current_user.template_name 
    self.class.layout "#{template_name}/application" 
    end 
end