I am trying to figure out how to set the layout from a custom made responder. I want to use the request.xhr? to set the layout for render to 'ajax'. Does anybody know how to do that? I am using Rails 3 and I have a responder like this:
module AjaxLayoutResponder
def to_html
if request.xhr?
# do something here to change layout...
end
super
end
end开发者_运维技巧
It seem to me like a responder is the best way to accomplish this 'ajax' layout switching.
I disagree that a responder is the way to go. Here's an easy solution that I use in most of my projects (however I just set the ajax layout to nil):
In application_controller.rb
layout :set_layout
def set_layout
request.xhr? 'ajax' : 'application'
end
you could simply do this:
module AjaxLayoutResponder
def to_html
if request.xhr?
options[:layout] = 'ajax'
end
super
end
end
because what is called at the end of responder execution is:
# from https://github.com/plataformatec/responders/blob/master/lib/action_controller/responder.rb
def default_render
if @default_response
@default_response.call(options)
else
controller.render(options)
end
end
精彩评论