开发者

Rails 3 set default actions for response

In my application i need to set some deafult actions for all format.js and format.htm responses. At this moment I have something like this in all controllers:

def index
  @users = User.all

  respond_to do |format|
    for开发者_如何学Cmat.html {html_response}
    format.js {js_response}
  end
end

But I think that it isn't a good solution. What can I do?


Make a private method in your ApplicationController and call it from wherever required

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜