recently i encountert a problem in IE and other browser, which i also noticed on railscasts.com some time ago.
For example, loading the page:
- railscasts.com/episodes/some_id#comments
would load a railscasts episode with the comment tab activated. navigating to the "similar" tab would change the url to:
- railscasts.com/episodes/some_id#similar
if i would click on the back or (IE) reload button, i would see the json representation for the comments.
Digging in the log file i found this:
Chrome:
- EpisodesController#show as HTML
- EpisodesController#show as HTML
IE
- EpisodesController#show as HTML
- EpisodesController#show as */*
in an related stackoverflow question it was suggested to change the order respond_to
block and make the first respond block 'html'
respond_to do |format|
format.html
format.json
end
meaning, if no respond format is given it would take the first one to respond with. not a DRY solution for the problem. also not possebly if you are using respond_with(@episodes)
another solution is to set the default response format in the routes.rb:
match 'episodes/:id' => 'episodes#show', :defaults => { :format => 'html' }
which is also not DRY because you dont want to write that for every route you define.
Lastly i found a suggestion to use a before_filter in the application controller:
before_filter :set_default_response_format
protected
def set_default_response_format
request.format = "html" if request.format == "*/*" && request.content_type.nil?
end
which seems a stable solution for that problem.
It seems weird that not more programmer encounter that problem, or am i doing sth. wrong in the first place ?