Last active
January 25, 2022 23:56
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p>Hi! I'm an about page. Edit me in views/static_pages/</p> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add to config/initializers | |
ALLOWED_STATIC_PAGES = Dir.entries("#{Rails.root}/app/views/static_pages").map {|file| file if file.include?('erb')}.compact |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example route | |
get '/about', to: 'static_pages#show', page: 'about' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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