Skip to content

Instantly share code, notes, and snippets.

@briandoll
Created January 22, 2011 06:12
Show Gist options
  • Save briandoll/790915 to your computer and use it in GitHub Desktop.
Save briandoll/790915 to your computer and use it in GitHub Desktop.
class SomeController < ApplicationController
# http://blog.jetthoughts.com/2010/12/22/allow-multiple-access-control-requests-for-rails/
before_filter :set_access_control_headers
def show
@thing = Thing.find(params[:id])
respond_to do |format|
format.html
format.json do
body = render_to_string(:locals => {:thing => @thing})
render :json => "{ \"html\" : \"#{to_json_value(body)}\"}", :callback => params[:callback]
end
end
end
# this could either go in ApplicationController or
# create an instance method on String
def to_json_value(str)
str.gsub!("\"", "\\\"")
str.gsub!(/\n+/, " ")
str
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment