Static Pages In Rails
In any web application there will be some static content. In the application I'm building for the Rumble, I needed a static landing page, as well as simple FAQ and Contact pages. I wanted simple static files that I could write in Haml (my choice of templating engine for this project).
The controller below allows you to keep static pages in
app/views/pages/<page>.html.haml
# app/controllers/pages_controller.rb
class PagesController < ApplicationController
rescue_from ActionView::MissingTemplate, :with => :invalid_page
def show
render params[:id]
end
def invalid_page
redirect_to root_path
end
end
A custom route allows us to link to static pages using page_url(:welcome)
.
The :requirements
on the route prevent malicious user input.
# config/routes.rb
map.page 'pages/:id',
:controller => 'pages',
:action => 'show',
:requirements => { :id => /[a-z]+/ }
Comments