Intuition:
This is happening because requests are being handled sequentially.
Say you have two requests. One is to view page A, and the other is to view page B.
However, the request to view page B cannot be fulfilled until viewing page A is done.
In your case, you created a dependency: page A cannot finish rendering until page B renders. However, page B won't render until all previous requests (in this case, the request to render page A) are completed.
In short, completing the read request depends on the requested page loading.
What's happening here:
Your request to render the JSON cannot finish until the original request is complete - so the process hangs.
Eventually, it waits too long and just times out.
This happens because unlike other languages (e.g. Javascript), Ruby requests are blocking when run on a single thread (i.e. synchronous, as opposed to asynchronous).
Solution:
Note that if you were running multiple processes or threads, this would work. See this SO answer..
Instead of spawning threads, however, you could just render the file directly to string with the render_to_string
method in Rails.
Good luck.