How to configure routes.rb so that all get requests are sent to a static index.html file located in the public folder?
- Y Why then do Rails? :) - D-side
- All for API .... - Pavel
- So still not all? - D-side
- I'm trying to do a SPA on React and screw this thing to Rails. I love Rails for its simplicity and clear code, and in version 5, and now you can only do the application API with ease. So I'm experimenting =) - Pavel
2 answers
Rots for static files, as far as I know, does not happen. Rails first check the presence of urla in the routs, if not - try to give static. Accordingly, if all routes are removed from routes.rb , the application will respond to / and index.html that very static file. But to make it so that it returns in response to ALL get-requests using Rails is problematic. Yes, and do not need it for the SPA. Usually, for this purpose, one path is actually used, which may have different arguments / anchors / other. Here's an example from Google: https://www.google.ru/#q=SPA .
Routes can be sent only to the controller or to 404. That's it.
But you can give static files from the controller. Therefore, the most “frontal” way is to make a “dummy” controller with one method consisting of a send_file call.
With proper configuration, you can use the features of the reverse proxy implementation so that Rails gives the path to the file sent to the proxy, and already gives the file to the proxy (it can usually do this more efficiently). On nginx this is done through the X-Accel-Redirect header. Accordingly, all routes (after the API, the lower the lower the priority), send there using match . But this is a bad way , although some of its parts may be needed elsewhere.
It is better not to bring the request to the application server and the route system in general , but choose a certain prefix / subdomain for requests to the API, the same for statics, and give the rest to index.html . And how to do this, look for the name of your proxy (for example, nginx ) documentation or articles.