Home PageSocialsBlog MetaPoetryTechThoughts/RantsProjectsVPS SetupGit ReposFriends of STUTS

Tech

Rails Quickstart - Static Pages & User Manager

×

Basic Setup

Create new rails app (note: the nokogiri line is usually needed on MacOS to properly build the dependency)

gem install nokogiri -- --use-system-libraries --with-xml2-include="$(xcrun --show-sdk-path)/usr/include/libxml2"
rails new . --database=postgresql
rails db:create

Launch server (check it’s running by going to http://localhost:3000)

rails s

Add some gems to Gemfile

# User management
gem 'devise'
# Bootstrap
gem 'bootstrap', '~> 4.0.0'  
# Font Awesome Icons  
gem "font-awesome-rails"  
   
# In 'group :development do' add  
gem 'rails-erd'

Install gem dependencies

bundle install

Bootstrap support

vim app/assets/stylesheets/application.css
   @import "bootstrap";
   @import "font-awesome";

Rename app/assets/stylesheets/application.css to app/assets/stylesheets/application.scss and remove require lines

Static Page Support

Static pages

rails generate controller pages --skip-assets

Add show method to the static pages controller at app/controllers/pages_controller.rb

def show
render template: "pages/#{params[:page]}"
end

Edit config/routes.rb to manage static page routing and set root path

get "/:page" => "pages#show"

root "pages#about"

Create app/view/pages/about.html.erb, this will be used as the new index page

Add a User Controller

Install devise things

rails g devise:install

Setup mail configuration for development environment in config/environments/development.rb

config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

Modify flash notification styles in app/view/layouts/application.html.erb (in the <body> of the page - remove the backslashes, that’s just me working around formatting issues with nanoc)

\<\% flash.each do |name, msg| \%\>
\<\%\= content_tag(:div, msg, class: "alert alert-info text-center") \%\>
\<\% end \%\>

Create user manager and views

rails g devise User
rails g devise:views
rails db:migrate

Additional user fields (this is optional but an example of adding attributes)

rails generate migration add_fields_to_users display_name:text website:text bio:text
rails db:migrate

Hope this helps you, Dan.