How to do static content in Rails?

后端 未结 8 925
孤城傲影
孤城傲影 2020-11-30 17:47

Looking at different options:

One is to just put the static pages in the public/ folder, but I do want the header from layout/application to be consistent.

I

8条回答
  •  -上瘾入骨i
    2020-11-30 18:26

    For Rails6, Rails5 and Rails4 you can do the following:

    Put the line below at the end of your routes.rb

      get ':action' => 'static#:action'
    

    Then requests to root/welcome, will render the /app/views/static/welcome.html.erb.

    Don't forget to create a 'static' controller, even though you don't have to put anything in there.

    Limitation: If somebody tries to access a page that does not exist, it will throw an application error. See this solution below that can handle 404s

    For Rails3 you have to use 'match' instead of 'get'

      match ':action' => 'static#:action'
    

提交回复
热议问题