Skip to content

Instantly share code, notes, and snippets.

@toadkicker
Last active January 25, 2022 23:56
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toadkicker/e3aa35677c5e10004e965e48bea5e623 to your computer and use it in GitHub Desktop.
Save toadkicker/e3aa35677c5e10004e965e48bea5e623 to your computer and use it in GitHub Desktop.
How to build the simplest static site with Rails (yes I know you can put files in public and call that simpler. This uses the framework)
<p>Hi! I'm an about page. Edit me in views/static_pages/</p>
# Add to config/initializers
ALLOWED_STATIC_PAGES = Dir.entries("#{Rails.root}/app/views/static_pages").map {|file| file if file.include?('erb')}.compact
# Example route
get '/about', to: 'static_pages#show', page: 'about'
class StaticPagesController < ApplicationController
def show
if valid_page?
render template: "static_pages/#{params[:page]}"
else
render file: "public/404.html", status: :not_found
end
end
private
def valid_page?
ALLOWED_STATIC_PAGES.include? "#{params[:page]}.html.erb"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment