Question

How do I match all .html files with High Voltage's controller in Rails so that any url with .html at the end opens the corresponding pages/<page>.html.erb file in my views directory? And of course then I can reference it with High Voltage's page_path named route?

Was it helpful?

Solution

I don't think it's possible, or at least trivial. The "suffix" part of the URL is mapped to the format of the request, e.g., /pages/document.html, /pages/document.json, /pages/document.xml, /pages/document.csv, etc. That is fundamental to Rails routing, and .html is the default format, so /pages/document will, by default, return HTML. It might be possible to create a route such as:

match '*.html' => 'high_voltage/pages#show', as: :static

I have not tested it though.

If you put the match at the bottom, it might be the case that even if the incoming url has .html on it, it will match something else before. Conversely, if you put it at the top, it might match everything that does not have a suffix. Should be easy enough for you to test it.

My high voltage route:

get '/:id' => 'high_voltage/pages#show', as: :static

is at the bottom of my routes, right before the root url, so it's basically the catch all. As long as my static html file paths are distinct from my RESTful routes, it works fine.

Otherwise, if it's acceptable for those static URLs to redirect (permanent), you could put the match in the upstream HTTP server, like nginx or apache, and redirect it to a known high voltage path, like /pages.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top