Starting New Rails Webpage

These are just my shorthand notes for how to start a new webpage with rails. Many of the pages I write have very similar features, so I wanted to have the list of commands that I usually use written down somewhere.

Make the new project

$ rails new basic_rails_project
$ cd basic_rails_project

Create the databases on both my laptop (also make test) and the server

$ mysql -u user -p
mysql> create database db1;
mysql> grant all privileges on db1.* to [email protected] identified by 'password';
mysql> create database db1_test;
mysql> grant all privileges on db1_test.* to [email protected] identified by 'password';
mysql > quit;

    adapter: mysql2
    encoding: utf8
    database: db1
    username: user
    password: password
    adapter: mysql2
    encoding: utf8
    database: db1_test
    username: user
    password: password

    adapter: mysql2
    reconnect: true
    encoding: utf8
    database: db1
    username: user
    password: password

I usually add the following gems to my Gemfile. Also make sure to uncomment therubyracer gem.

gem 'capistrano', '2.15.4' # I prefer version 2 which is simpler than version 3
gem 'authlogic'
gem 'declarative_authorization'
gem 'paperclip'
gem 'mysql2', '0.3.20'
gem 'kaminari'
gem 'ransack'
gem 'net-ldap'
gem 'prawn'
gem 'twitter-bootstrap-rails'
gem 'less-rails'

$ bundle

Make a Homepage

$ rails generate controller homes show --skip-javascripts --skip-stylesheets

Don't need a model for homes as it's usually just a landing page. So create views/show.html.erb.

Add to routes.rb, resources :homes, only: show

Users table and logins

$ rails g scaffold user username:string firstname:string lastname:string persistence_token:string last_request_at:datetime role:string --skip-javascripts --skip-stylesheets
$ rails g controller user_sessions new create destroy --skip-stylesheets --skip-javascripts
$ echo "class UserSession < Authlogic::Session::Base" > app/models/user_session.rb
$ echo "end" >> app/models/user_session.rb

Scaffolding here works great. All I usually do it delete the extra json stuff in the controller.

Make a settings table
This table will only have one entry. It holds items about the state of the app. For example, there will be a checkbox accepting, telling us whether or not we're accepting more registrations. This is basically my admin table. It will be created with a seed file and then just edited as necessary.

$ rails g controller settings index edit update --skip-stylesheets --skip-javascripts
$ rails g model setting active:boolean home_page_text:text link_text:string accepting:boolean
$ echo "Setting.create(active: 'false', home_page_text: 'Under construction', link_text: 'Coming Soon', accepting: 'false')" >> db/seeds.rb

Set up bootstrap

$ rails g bootstrap:install

Set up git

Login to github and create a new repo named basic_rails_project.

$ git init
$ git remote add origin

Normally I'd edit .gitignore and not include files with sensitive information in the repo. Things like database.yml, secrets.yml, environment files that might have email info, etc. For this example, I'm including everything, since this app is never going to run on a real server.

$ git add .
$ git commit -a
$ git push -u origin master
Username for '': maryheintz	
Password for 'https://[email protected]': 

And for those of you interested, you can find my rails app with all the files that I edited on Github.