How to do static content in Rails?
depends on the url structure, if you want the paths to come off of / (e.g. /about_us), then:
map.connect ':action', :controller => "static"
This should go at the very end of your routes file, Throw your .html.erb files into app/views/static and you are done.
e.g: throwing in about_us.html.erb
, will give you a page at /about_us.
The item that you have in your question is great for a catch all route where you can analyze the array given to you at params[:path]
. A bit more information on that at http://railscasts.com/episodes/46-catch-all-route
thoughtbot has a plugin called high_voltage for displaying static content: https://github.com/thoughtbot/high_voltage
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'