Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Tell us about yourself.
Run these commands in the terminal. You should expect them to return a number, like '4.0' or '2.0.0'.
$ ruby -v $ rails -v
Navigate to a project directory in your terminal, then run:
$ rails new TestApp $ cd TestApp $ rails server
Now, visit localhost:3000 in your browser to view your app!
We told Rails to build a new app called TestApp, then navigated into it and started a server so we could view it live in the browser. Sweet.
Note: you can shut the server down by entering Ctrl-c
In the terminal, generate the model, then run database migration:
$ rails generate model student bio:string full_name:string $ rake db:migrate
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!
#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
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.
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:
In config/routes.rb, set the root route to our students#index controller:
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.
Blogger tutorial from Jumpstart Labs, Section I0 only!