Routing to static html page in /public

Ruby on-RailsRuby on-Rails-3UrlRouting

Ruby on-Rails Problem Overview


How can I route /foo to display /public/foo.html in Rails?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

You can do this:

Add this, into your routes.rb file.

match '/foo', :to => redirect('/foo.html')

Update

In Rails 4, it should use "get", not "match":

get '/foo', :to => redirect('/foo.html')

thanks Grant Birchmeier

Solution 2 - Ruby on-Rails

This can be done without triggering a redirect. Follow the steps further down to be able to route static files in config/routes.rb as shown in this example:

# This route will serve public/index.html at the /login URL 
# path, and have a URL helper named `login_path`:
get "/login", to: static("index.html")

# This route will serve public/register.html at the /register
# URL path, and have URL helper named `new_user_registration_path`:
get "/register", to: static("register.html"), as: :new_user_registration
  1. Install the rails-static-router gem: https://github.com/mufid/rails-static-router#installation
  2. Restart app (first bin/spring stop to be sure app is completely reloaded).
  3. Start using the static(path) method in your config/routes.rb.

Solution 3 - Ruby on-Rails

E.g in Rails 4 add the following route:

get '/example', :to => redirect('example.html')

Also you need to enable static files from the 'public' directory in your configuration:

config.serve_static_files = true

OR

config.serve_static_assets = true

Also you might need to provide your public directory as root in NGINX configuration.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAen TanView Question on Stackoverflow
Solution 1 - Ruby on-RailsArkanView Answer on Stackoverflow
Solution 2 - Ruby on-RailsEliot SykesView Answer on Stackoverflow
Solution 3 - Ruby on-RailsАлександр ТихоновичView Answer on Stackoverflow