Commit 3a81fa16 authored by Faisal Al-Tameemi's avatar Faisal Al-Tameemi

songs and albums sinatra example breakout

parent d129994e
...@@ -2,3 +2,13 @@ ...@@ -2,3 +2,13 @@
get '/' do get '/' do
erb :index erb :index
end end
get '/songs' do
@songs = Song.all
erb :songs
end
get '/albums' do
@albums = Album.all
erb :albums
end
class Album < ActiveRecord::Base
has_many :songs
end
class Song < ActiveRecord::Base
belongs_to :album
end
<h1>
Albums
</h1>
<ul>
<% @albums.each do |album| %>
<li> <%= album.title %> </li>
<% end %>
</ul>
...@@ -18,6 +18,10 @@ ...@@ -18,6 +18,10 @@
<title></title> <title></title>
</head> </head>
<body> <body>
<nav>
<li>Nav item 1</li>
<li>Nav item 2</li>
</nav>
<%= yield %> <%= yield %>
</body> </body>
</html> </html>
<h1>
Songs
</h1>
<ul>
<% @songs.each do |song| %>
<li> <%= song.name %>, <%= song.album.title %> </li>
<% end %>
</ul>
class Songs < ActiveRecord::Migration
def change
create_table :songs do |s|
s.string :name
s.string :artist
s.integer :likes
end
end
end
class Albums < ActiveRecord::Migration
def change
create_table :albums do |al|
al.string :title
al.string :record_label
al.date :release_date
end
end
end
class SongsForeignKey < ActiveRecord::Migration
def change
add_column :songs, :album_id, :integer
end
end
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead # This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to # of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition. # incrementally modify your database, and then regenerate this schema definition.
...@@ -10,11 +11,19 @@ ...@@ -10,11 +11,19 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20140321144528) do ActiveRecord::Schema.define(version: 20160516195607) do
create_table "users", force: true do |t| create_table "albums", force: :cascade do |t|
t.string "title"
t.string "record_label"
t.date "release_date"
end
create_table "songs", force: :cascade do |t|
t.string "name" t.string "name"
t.string "email" t.string "artist"
t.integer "likes"
t.integer "album_id"
end end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment