I generate a diagram in Rails using the d3.js JSON callback as follows:
View
d3.json(document.URL, function(data){ // generate chart }
controller
def index respond_to do |format| format.html do # return the HTML end format.json do # return the JSON end end end
Everything works perfectly. However, when the user leaves this diagram and then navigates to it using the back button on their browser, they are presented with JSON, not HTML.
Can you suggest how I can fix this?
d3.json(window.location.pathname + ".json" + window.location.search.substring(0), function(json){ // generate chart }
How inspired this question .
, , "", URL-. - JSON, AJAX (D3).
- .html URL . , ApplicationController:
.html
ApplicationController
before_filter do if request.format == :html && !params[:format] redirect_to format: :html end end
Vary: Accept, - Chrome , .
Vary: Accept