Routing to static html page in /public

后端 未结 3 1231
北海茫月
北海茫月 2020-12-07 16:06

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

3条回答
  •  情话喂你
    2020-12-07 17:08

    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.

提交回复
热议问题