This will be setup in postgreSQL
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."
- Select 'New repository' or navigate to https://github.com/new
- Create the repo on github. Don't add a .gitignore or license.
- 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
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'
bundle install
be rails generate rspec:install
git add .
git commit -m "Set up rspec, pry [and names of other gems]"
git checkout -b feature_branch
Open NEW console tab/window in the same rails directory =>
rails server
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
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
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
Set up basic routes in Controller files. Boilerplate setup usually has the following methods: index, show, new, edit, create, update, destroy
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'
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)
git add .
git commit -m "Set up initial migrations, models, controllers, routes"
create index/show/new etc files as needed in sub folders as needed as name.html.erb files
Make sure bcrypt is in gemfile and bundle installed
rails generate model user
rails generate controller users
rails generate controller sessions
has_secure_password
t.string :email, null: false
t.string :password_digest
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
def logged_in?
!current_user.nil?
end
rake routes
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"
create file "results.html.erb"
def results
@first_question = Question.first
end
Add any variables that are used in the results page
get 'results' => 'users#results'
rails generate controller pages about
this will automatically create a pages controller, pages view folder with about.html.erb file
Methods def about & def resources will be created automatically, they can be left blank
get 'about' => 'pages#about'
get 'resources' => 'pages#resources'