Modified from GDI Austin Ruby on Rails track. Major, major props to Cecy Correa of GDI Austin!
Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Some "rules"
Tell us about yourself.
You can use Cloud9 or Rails "locally" if you already have it installed.
We are going to be using Cloud9, so please sign up!
Tons of different gems you can install to provide standard functionality in your app!
Rails already comes "pre-configured" with a lot of stuff so you don't have to spend time doing set-up.
This means you have to follow convention.
The downside is — you have to be careful with what you do / move.
For example, you wouldn't want to delete a folder you don't think you're going to use.
It separates the data needed to power a UI from the representation of that data.
Model: the brains (like a class!)
Controller: dispatcher
View: what the user sees
Model: ingredients of a recipe
Controller: directions of a recipe
View: picture of a recipe
The Model, View, and Controller all go hand in hand.
rails new app
You just created a new Rails app called "app".
You can call it whatever you want!
rails new bananas
^^That creates a new Rails app called "bananas"
For now, you really only want to be concerned with:
For every page of your web app:
Data, Controller, View, Route
To create your model and migration file:
rails generate model Chirp
To create your database table from the migration:
rake db:migrate
Active Record object
Active Record is an "Object Relational Mapping" framework included with Rails. It maps database tables to Ruby objects for us.
To test out the model:
rails console
Chirp.all #brings up all Chirps. You’ll notice it’s an array!
Chirp.first #brings up the first Chirp in the database
Chirp.last #brings up the last Chirp in the database
Chirp.delete_all #deletes all the Chirps in the database (be careful!)
Chirp.delete(x) #deletes a specific Chirp in the database corresponding to the id.
rails generate controller Chirp index
Rails convention: index, show, update, create, destroy
Rails convention: index.html.erb, edit.html.erb, new.html.erb, show.html.erb
Uses erb! (HTML file with embedded Ruby)
Common Helpers:
If you want to learn more about how Rails automatically bundles up all these HTML, CSS, and Javascript files: Rails Asset Pipeline
get ‘chirps/new’, to: ‘chirps#new'
post ‘/chirps’, to: ‘chirps#create’
get ‘chirps/:id’, to: ‘chirps#show’, as: :chirp
delete '/chirps/:id', to: 'chirps#destroy'
root ‘chirps#index
Start your Rails server using:
rails server
Test out your app by navigating to localhost:3000 if developing locally or launch app via Cloud9
What we just built is called "CRUD"
CREATE
READ
UPDATE
DELETE
This is the most common type of app.
A blog
Pretty much any app needs basic CRUD
rails generate scaffold Chirp username:string post:text
This command generates:
Because it's important to know the underlying factors of how things work.
Now you can spend your time being more productive!
Rails does a ton for you out of the box!
Learn more about the Rails request/response lifecycle here
Using the Rails framework allows you to focus on building your app!
Instead of all the plumbing required to get a web application up and running
Gems are "packages" in Ruby that do things for you.
There are a lot of gems you can take advantage of.
This is great when you're learning! You can see the gem's source code and see how they implemented the feature.
When choosing a gem, make sure that:
Open your "Gemfile" (located at the root of the app)
gem 'name-of-gem'
Usually, the gem documentation will say how to do this
Run 'bundle install' on the command line to install your gems