Skip to content

Instantly share code, notes, and snippets.

@chand
Last active August 26, 2017 15:29
Show Gist options
  • Select an option

  • Save chand/04cf1630551752b98fac411eacf8d11b to your computer and use it in GitHub Desktop.

Select an option

Save chand/04cf1630551752b98fac411eacf8d11b to your computer and use it in GitHub Desktop.
Ruby on Rails New App Setup Instructions

Ruby on Rails New App Setup Instructions

New App Setup

This will be setup in postgreSQL

In Command line

rails new my_great_app -T -d postgresql --skip-turbolinks
cd my_great_app
git init (if this isn't already git)
git add .
git commit -m "Initial commit. Rails boilerplate."
  1. Select 'New repository' or navigate to https://github.com/new
  2. Create the repo on github. Don't add a .gitignore or license.
  3. Back in your command line, inside your project folder
git remote add origin https://github.com/your_organization/my_great_app.git (this url comes from the new github repo)
git push -u origin master

In Gemfile

Comment out gem 'coffee-rails' Uncomment the reference to bcrypt or add devise gem under "group :development, :test do" add:

gem 'rspec-rails', '~> 3.4'
gem 'pry-rails'

In Command Line

bundle install
be rails generate rspec:install
git add .
git commit -m "Set up rspec, pry [and names of other gems]"

In Command Line

git checkout -b feature_branch

Open NEW console tab/window in the same rails directory =>

rails server 

In Command Line:

rails generate controller name
rails generate model name (this creates model AND migration file)

It does not matter if it is plural or not, it will automatically generate the name correctly Repeat this as many times as needed

In Sublime > db > migrate > (migration file):

create columns in table, ie t.string :title, null: false etc. Example:

class CreateResponses < ActiveRecord::Migration
  def change
    create_table :responses do |t|
      t.string :response, null: false
      t.references :user, null: false
      t.references :question, null: false

      t.timestamps null: false
    end
  end
end

In Sublime > app > models > (model file):

Add the has/many/belongs to etc, ie:

belongs_to :survey
has_many :responses
has_many :users, through: :responses
validates :question, :survey_id, presence: true

In Sublime > app > controllers > (controller file):

Set up basic routes in Controller files. Boilerplate setup usually has the following methods: index, show, new, edit, create, update, destroy

In Sublime > config > routes.db

root "name#index" (sets up which route to show as the index/homepage)

add resources (ie: resources :posts) to each table name, nest if one belongs to the other, ie:

resources :posts do
  resources :comments
end

Add other routes such as:

get 'signup'=> 'users#new' 

resources :users

get 'login'  => 'sessions#new' 
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'

In command line

When initial models/migrations/routes are created and set up

be rake db:create && be rake db:migrate && be rake db:seed (if there is a seed file)

In the browser (make sure rails intro page shows up without errors):

http://localhost:3000/

In the command line

git add .
git commit -m "Set up initial migrations, models, controllers, routes"

In Sublime > app > views > ..

create index/show/new etc files as needed in sub folders as needed as name.html.erb files

Adding Basic User Authentication

Make sure bcrypt is in gemfile and bundle installed

In Command Line

rails generate model user 
rails generate controller users
rails generate controller sessions

In Sublime > App > Models > User

has_secure_password

In Sublime > db > migrate > XXXXXX_create_users:

t.string :email, null: false
t.string :password_digest

In Sublime > app > controllers > application_controller.rb:

helper_method :current_user 

def current_user 
  @current_user ||= User.find(session[:user_id]) if session[:user_id] 
end

def require_user 
  redirect_to '/login' unless current_user 
end

In sublime > app > helpers > SessionsHelper:

def logged_in?
  !current_user.nil?
end

To view all routes for your project, in the command line

rake routes

To add columns to tables without dropping and remigrating, in the command line

rails generate migration add_email_to_users email:string (add_columnname_to_tablename)
rake db:migrate

This creates a new migration file in Sublime > db > migrations. Add input type (ie string) and any other info for example:

  change_table(:table_name) do |t|
    t.column :column_name, :string, null: false, default: whatever
  end

To create new pages in existing models/views that are not columns in the database, for example, "results" under "users"

In Sublime > app > views > users:

create file "results.html.erb"

In Sublime > app > controllers > users_controller.rb:

def results
  @first_question = Question.first
end

Add any variables that are used in the results page

In Sublime > config > routes.rb =>

get 'results' => 'users#results'

To create new pages that don't belong to any particular model (ie "about)

In command line:

rails generate controller pages about

this will automatically create a pages controller, pages view folder with about.html.erb file

In Sublime > app > controllers > pages_controller.rb

Methods def about & def resources will be created automatically, they can be left blank

In Sublime > config > routes.rb:

get 'about' => 'pages#about'
get 'resources' => 'pages#resources'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment