Boxy.js, XHR, Ruby on Rails
I'm using Boxy.js to call my /signup page into a modal window. I suppose it's Ajax. I want the page to not render with layout when it's being called this way.
I tried:
layout proc {|controller| controller.request.xhr? ? false: "application" }
and:
def render(*args)
args.first[:layout] = false if request.xhr? and args.first[:layout].nil?
super
end
in application controller, snippets I googled but they don't work. /signup is still coming up with the layout.
Here's part of the headers when /signup is called by Boxy:
Request URL:http:/开发者_运维百科/localhost:3000/signup?_=1301708866195
Request Method:GET
X-Requested-With:XMLHttpRequest
Best practice is to use Rails' js response, like so:
respond_to do |format|
format.html
format.js { render :action_name, :layout => false }
end
But because jQuery won't send the proper XHR headers by default, you'll need to add something like this to your application.js:
$.ajaxSetup({
headers : {
'Accept' : 'text/javascript',
'X-Requested-With' : 'XMLHttpRequest'
}
});
If you'd rather just use something quick and dirty, try this:
respond_to do |format|
format.html { render :layout => false if request.xhr? }
end
精彩评论