2011-04-28 38 views
1

在我的應用程序中,我需要爲所有format.jsformat.htm響應設置一些deafult操作。在這一刻,我在所有的控制器是這樣的:Rails 3爲響應設置默認操作

def index 
    @users = User.all 

    respond_to do |format| 
    format.html {html_response} 
    format.js {js_response} 
    end 
end 

但我認爲,這不是一個很好的解決方案。我能做什麼?

+0

以及如何做'html_response'和'js_response'目光從打電話了嗎? – nathanvda 2011-04-28 12:38:11

回答

2

做一個私有方法在ApplicationController何地需要

class ApplicationController < ActionController::Base 
    … 
    private 

    def default_responses 
    respond_to do |format| 
     format.html {html_response} 
     format.js {js_response} 
    end 
    end 
end 


class SomethingsController < ApplicationController 
    def index 
    @somethings = Something.all 
    default_responses 
    end 
end 
相關問題