가장 쉬운 방법은 Ruby가 전체 응답을 구문 분석하고 after_filter
을 사용하여 다시 응답하도록하는 것입니다. app/controllers/application_controller.rb
에 다음 코드를 입력하십시오.
class ApplicationController < ActionController::Base
after_filter :format_json
private
def format_json
if request.format.json?
json = JSON.parse(response.body)
response.body = params[:pretty] ? JSON.pretty_generate(json) : json.to_json
end
end
end
: 당신이 요청이 매개 변수를 사용하여 지정할 수 수
class ApplicationController < ActionController::Base
after_filter :beautify_json
private
def beautify_json
response.body = JSON.pretty_generate(JSON.parse(response.body)) if request.format.json?
end
end
또는 : 당신은 당신이 대신 축소 된의 아름답게 json으로 원하는 것을 결정하는 경우
class ApplicationController < ActionController::Base
after_filter :minify_json
private
def minify_json
response.body = JSON.parse(response.body).to_json if request.format.json?
end
end
,이 코드를 사용할 수 있습니다