Skip to content

Instantly share code, notes, and snippets.

@ahoward
Created December 16, 2008 17:12
Show Gist options
  • Save ahoward/36666 to your computer and use it in GitHub Desktop.
Save ahoward/36666 to your computer and use it in GitHub Desktop.
# generates a div that will load itself with content from an ajax request.
# the request is un-altered so, if you don't want it wrapped in the
# header/footer you may need to pass in 'partial=true' with the query string
#
# eg: ajax_content_for :url => '/content/some-path'
#
# eg: ajax_content_for '/content/some-path'
#
def ajax_content_for *args
options = args.options
options.to_options!
url =
if options[:url]
options[:url].to_s
else
args.join
end
slug = url.slug
id = "content-for-#{ slug }"
selector = "##{ id }"
<<-html
<div id=#{ id.inspect }>&nbsp;</div>
<script type="text/javascript">
(function(){
jq.ajax({
'url' : #{ url.to_json },
'async' : false,
'success' : function(data, status){ jq(#{ selector.to_json }).html(data) },
'error' : function(data, status){ jq(#{ selector.to_json }).html('Sorry, ' + #{ url.to_json } + ' not found') },
});
})();
</script>
html
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment