slides: http://gdibtv.github.io/gdi-rails
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.
https://c9.io/web/login
If you have a Github account, connect
Pick "RailsTutorial.org"
Run these commands in the terminal.
$ ruby -v
> /usr/local/rvm/rubies/ruby-2.2.1/bin/ruby
$ rails -v
> Rails 4.2.1
Navigate to a project directory in your terminal, then run:
$ rails new test_app
$ cd test_app
$ rails server --binding $IP --port $PORT
Visit in the browser to view your app!
http://<workspacename>-<username>.c9.io
https://rails-tutorial-tester-joshuaburke.c9.io/
In the terminal, generate the model
$ rails generate model student bio:string full_name:string
> invoke active_record
create db/migrate/20150623144213_create_students.rb
create app/models/student.rb
invoke test_unit
create test/models/student_test.rb
create test/fixtures/students.yml
$ rake db:migrate
> == 20150623144213 CreateStudents: migrating =============
-- create_table(:students)
-> 0.0015s
== 20150623144213 CreateStudents: migrated (0.0016s) ====
Migrations are Ruby code that translate to SQL code. They’re how we setup, configure, and tune our database.
Some migrations are generated for you when you, for example, generate a new model. You will also create many of your own independently, to make changes to your database schema (adding or removing columns, changing names of columns, etc).
Find them in db/migrate/
Your app will not work as expected if you have pending migrations. They must be ‘run’ to ensure that your database is properly setup. Run them each as they’re created; run them all when resetting your database or installing the app on a new computer. rake db:migrate
In the terminal, enter the rails console:
$ rails console
Then, create a Student object:
> Student.create(full_name: "Hermione Granger",
bio: "Dedicated student")
Note: close the console by entering the word 'quit'.
The Rails console is like IRB with your Rails app loaded in. Rad!
CRUD Operations:
#Create
> Student.create
> Student.create(full_name: "Bert", bio: "Bestie of Ernie")
#Read
> Student.all
> Student.first
> student = Student.find(1)
#Update
> student.update_attributes(full_name: "Bert Bernard")
> student.update(bio: "Scrabble champ of Sesame Street")
#Destroy
> student.destroy
In the terminal
$ rails generate controller Students
> create app/controllers/students_controller.rb
invoke erb
create app/views/students
invoke test_unit
create test/controllers/students_controller_test.rb
invoke helper
create app/helpers/students_helper.rb
invoke test_unit
invoke assets
invoke coffee
create app/assets/javascripts/students.coffee
invoke scss
create app/assets/stylesheets/students.scss
Generating this controller also creates an app/views/students directory.
In your text editor, open the generated app/controllers/students_controller.rb, and add an ‘index’ method to the class definition:
def index
@students = Student.all
end
By isolating logic and presentation, you remove dependencies.
Views don’t need to know how their data is generated, and Models don’t need to know how their data is used. Changes to one won’t affect the other.
An app this basic doesn’t really have any logic to put in the model. The controller can handle basic database actions like finding Student.all.
Every view that your app has must have an associated controller method. This method will usually pass data to the view, but not necessarily.
In the app/views/students directory, create a file called index.html.erb. Insert into it:
<h1>Student List</h1>
<% @students.each do |student| %>
<%= student.full_name %>
<%= student.bio %>
<% end %>
ERB is Embedded RuBy. It’s a templating system that allows you to embed Ruby code into a text document, like HTML. It’s included in the standard Ruby library.
Syntax:
Ruby is processed, but does not display anything:
<% code %>
Ruby is processed, and outputs result to the page:
<%= code %>
In your text editor, open config/routes.rb, and create resource routes for our student controller:
resources :students
In config/routes.rb, set the root route to our students#index controller:
root 'students#index'
Routing for your app is controlled in the config/routes.rb file. The comments in this file are extremely helpful.
If a view doesn’t have a route, there’s no way for the browser to request and receive it.
Resource routes are a shortcut. They provide routes for all CRUD actions associated with a model, so you don’t have to spell them each out.
Practice:
Blogger tutorial from Jumpstart Labs, Section I0 only!