Created
June 15, 2009 21:51
-
-
Save pat/130373 to your computer and use it in GitHub Desktop.
Returning HTML in AJAX calls, without layouts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Three things to add: | |
# * before_filter call | |
# * action_has_layout? method (if you have one, combine them) | |
# * adjust_for_inline | |
# | |
class ApplicationController < ActionController::Base | |
# ... | |
before_filter :adjust_for_inline | |
# ... | |
private | |
# We don't want no stinking layout if it's an AJAX request. | |
def action_has_layout? | |
request.format != :inline && super | |
end | |
def adjust_for_inline | |
request.format = :inline if request.xhr? | |
end | |
# ... | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add this line to your existing file. | |
# Jury's out on the best name for the pseudo-mimetype though. | |
Mime::Type.register_alias "text/html", :inline |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A long time ago, in an action far far away | |
def index | |
# ... | |
respond_to do |wants| | |
wants.inline { | |
# ideally use your own view (index.inline.erb) | |
render :text => 'success!' | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment