Making a new Rails app, using the example dog adoption app created for RailsBridge Chicago 2015. Works through databases, migrations, models, controllers, and more basics.
What is Advanced Excel and what are some best practices for designing and cre...
RBC Mod 1: Making a New Rails App
1. Making a New
Rails App
RailsBridgeChicago Module 1
Ameeda Chowdhury
twitter.com/ameedahc
2. Create a new app by typing in the terminal command:
$ rails new <appname>
Use the terminal to create and destroy files and directories
for your app
Make a new app ‘railsbridgedogs’
3. These are the application
files and directories
created - see in terminal
Files generated for ‘railsbridgedogs’
5. Ruby Gems
*Gems* are self-contained Ruby programs and libraries
When you make a new app it fetches *gems*.
These gems are specified in your app’s Gemfile.
You need to install the gems specified in your Gemfile.
10. What is Git?
How do we use Git?
What happens when we don’t use Git?
Git is “version control” software that allows you:
1. show the changes that were made to the code over time
2. allows you to backtrack if necessary and undo those changes
3. enables team collaboration on code
Why do we need Git?
11. Last, commit your files with a name in quotes “...”.
These files, in their current state, will be accessible to
you any time in the future by commit name, no matter what
changes you make (in future commits).
First, initialize an empty git repository
note the output
Then, add all your app’s
files to the git repo
How do we use Git?
12. So… what is one object or thing you want users to be
able to access for your dog adoption app?
13. Resource is any object you want users to be able to
create, read, update, or delete by visiting its URL
Dog Resource!
14. A Rails
scaffold
quickly generates
a complete set of
files with basic code
for the named
resource
The 4 most important
scaffolded files for Dog
1. Database migrations
2. Model
3. View
4. Controller
Scaffold Dog Resource!
15. A database is needed to to store
any dog records we create.
We created a database on the
local machine.
Migration files allow you to modify
the database by adding or removing
tables, columns, and entries.
In this case, the migration file
produced by the Dog scaffold
is run, and the Dog table is created.
Databases & Rails Migrations
16. Databases & Rails Migrations
...maps to the name attribute of instances of your Dog model.
$ rails console let’s you interact with
your application from your terminal’s
command line.
The name database
column of each row of
your Dog table ...
17. The Dog model file will let
you manipulate data stored
in the dog database table.
The Dog model represents
the data the logic of dog
objects.
Rails Migrations -> Model!
18. The DogsController
controller file processes
the webserver’s requests
and makes the Dog model’
s data available to the
views.
Controller --> Model data in Views
19. Run a local webserver to see your webpages run
visit http://localhost:3000/dogs
This is showing us the app/views/dogs/index page
This page is empty because we
have not saved any dogs in our
database.
Create a few dogs with names!
Run Rails Server
20. Create a few dogs through this
form.
The fields for the dog’s name in the
new, edit, show, and index
are there because we included
‘name:string’ in our scaffolding.
Creating through View Forms
21. @dogs is available in the view.
each pulls out one dog instance at a time from
inside your collection of all dogs @dogs
app/views/dogs/index
@dogs is an instance variable inside the
index method storing the collection of all dogs
pulled from the database
app/controllers/dogs
Connecting Views to Controllers
<%= dog.name %> prints the dog’s name, once for each dog
because it’s inside the loop app/views/dogs/index