In this lab, you're going to make some sweet static websites. Static means no server is needed, so you're not using Rails or Sinatra. You're going to use ActiveRecord and ERB to generate regular html web pages.
Using ActiveRecord and ERB you'll create ERB
template files that end in .html.erb
where you can call your ActiveRecord models - Artist
, Genre
, and Song
- to
access all the information stored in your database.
In this lab, you'll be recreating the basic functionality of Playlister using
ActiveRecord associations. The point of your association is to let your
program know that an artist has many songs and therefore those songs belong to
that artist. An artist will also have many genres, which it will know of through
songs, so Artist.genres
You'll have three models: Artist
, Song
, and Genre
. The Artist
and
Genre
models will each know about its associated songs and each other. To
create these associations you will need to write proper migrations and
ActiveRecord macros, special methods created by ActiveRecord's DSL.
The model tests will be calling methods that your models will, by default, have
if your associations are correctly defined. You do not need to write those
methods. Once your associations are defined you can run rake db:seed
to
seed your database with sample data.
- Create your models in
app/models
- Your models will need an instance method called
to_slug
for making urls- ex.
an_artist.to_slug #=> kanye-west
- ex.
- Write migrations for your models and associations in
db/migrations
Embedded Ruby (ERB) is a way of running Ruby in non .rb
files. You'll be
making your own generator that will read an html.erb file to generate many
static pages.
Your template files will be in the following structure:
app/views
├── artists
│ ├── index.html.erb
│ └── show.html.erb
├── genres
│ ├── index.html.erb
│ └── show.html.erb
├── index.html.erb
└── songs
├── index.html.erb
└── show.html.erb
Your processed files will be in the following structure:
_site
├── artists
│ └── index.html
├── genres
│ └── index.html
├── index.html
└── songs
└── index.html
Each model will generally have an index page. Often they list all instances of a model.
- You will need to generate an index page for the top level of
app/views/
- The index should have a link to each model and a count of total instances
- ex.
Artists - 42
- ex.
- The index should have a link to each model and a count of total instances
- Your index pages should be called
index.html.erb
- Each model will have an index page in its appropriate folder in
app/views
directory- This index lists every instance and links to that instance's show page
An instance of a model will often have show.html.erb
that is rendered as a regular HTML file with information specific to that instance.
- Each model will have show pages in
app/views/<model>/
Artist
show pages will have the instance's name and a list of and link to its genres and songs.Genre
show pages will have have the instance's name and a list of and link to its artists and songs.Song
show pages will state the instance's name and names of and links to its artist and genre.
You will need a class whose sole purpose is generating the static pages.
- It should accept a path upon initialize to where it will save the files it generates
- It should use the ERB files from
app/views/
to generate all static pages - It will live in
lib/support
This lab provides you with several Rake tasks to help you along the way.
rake db:migrate
will automatically create the database and run your migrations; all you need to do is write the migrationsrake db:seed
will automatically parse the songs and seed your database; no work is required beyond migrationsrake console
will load a pry session with your environment loaded to play with the databaserake generate
will run your site generator once it is made assuming it has thegenerate
method
View Static Playlister on ActiveRecord on Learn.co and start learning to code for free.