Ruby on Rails basics [4d]
Rails is our web framework.
Goals
- Be able to write a simple Rails application.
- Understand how Rails talks to the database (
ActiveRecord
)- What is a model?
- How are records retrieved and saved?
- Validations
- Migrations
has_one
,has_many
,has_many :through
- Gain an understanding of the structure of a basic Rails app
- Routes
- Controllers
- Generate a controller using Rails scaffolding
- Write your own controller
- Views
- using
ERB
- using
- Models
- Helpers
- Learn how to do CRUD ("Create", "Read", "Update", "Destroy")
- How to show a list of existing database records
- How to build a user interface to show and update database records
Resources
- Rails for Zombies: Interactive introduction to Rails (very basic - low-level)
- Ruby on Rails Tutorial - Learn Web Development with Rails (it's in our library) : This (online) book by Michael Hartl is highly praised by beginners as well as experts.
- Gettings Started with Rails: First example app
- ActiveRecord guides: Guides for ActiveRecord (section "Models")
- apidock.com: Reference for Rails and Ruby
Exercises
Find your way around an existing app
Let someone help you get the code for makandracards.com.
Have a look around. There will be much you don't understand, yet; don't lose yourself in the details.
Try to see if you can understand a few basic things in the code:
- What are the important models? What model represents a "card" like this one
- How are cards associated with our "makandra dev" deck at https://makandracards.com/makandra?
- Where is the form to update a card? The controller? The view?
- There is a feature to "repeat cards", that brings random "repeating cards" to the top of the deck, once a week. Search for "repeating", and see if you understand what's happening.
Rails tutorial
Work through the Ruby on Rails Tutorial
(library), except for chapters 4.2 - 4.4, 9, 11 and 12.
For each chapter:
- You should do all the programming exercises.
- You can skip the testing exercises. There will be a later card to learn testing. We're also mostly using different testing frameworks and writing different types of tests than the tutorial.
- You can also skip the chapter "1.3 Version control with Git" and all deploying steps
In chapter 1.2 during the setup use Rails version 6.0.3.4, otherwise "rails new" might fail.
After you created your new rails app in chapter 1.2.1 of the tutorial above, open the file Gemfile
, check if this line exists and if so, delete it:
gem 'chromedriver-helper'
Write your own controller
Using a new or an existing app, create a model and a controller.
Then rewrite it according to the pattern from the chapter "Beautiful Controllers" in our book Growing Rails Applications in Practice (it's in our library).
Which one do you like better? Why?
Movie database (MovieDB)
Write a rails app that lets you manage a list of movies, and a list of actors.
Movies can have multiple actors (and hence an actor can be starred in multiple movies).
Hint:
- Keep the interface simple. For example, only allow the user to add a fixed number of actors to a movie at a time.