Making a Random Song App in Ruby on Rails: A Complete Beginner’s Guide

Matt Hinea
Aug 1, 2016 · 7 min read
rails new random_song_app
gem ‘rspotify’
$ bundle install
$ rails g controller Song
$ rails g migration Word entry:string
class CreateWords < ActiveRecord::Migration
def change
create_table :words do |t|
t.string :entry
t.timestamps null: false
end
end
end
dictionary = File.readlines('5desk.txt').each do |word|
Word.create(entry: word.chomp)
end
$ rake db:seed
root "song#index"
get '/', to: "song#index"
# or
get '/' => "song#index"
def indexend
$ touch app/views/song/index.html.erb
<%= render 'shared/nav' %>
<div class="container">
<div class="row">
<div class="col-lg-12">
<h2 class="grey">No DJs. No algorithms. No user data. Just a random song.</h2>
</div>
</div>
<div class="row">
<div class="col-lg-4 col-lg-offset-4">
<%= link_to "Get a song.", song_path, class: 'center', id: 'get-song' %>
</div>
</div>
</div>
<div class="container">
<div class="row">
<nav>
<div class="col-sm-3">
<h1><%= link_to "Spotify.sample", root_path, class: "center", id: "spotify-sample" %></h1>
</div>
<div class="col-sm-1 col-sm-offset-8">
<h2><%= link_to "about", about_path, class: 'center', id: 'about-link' %></h2>
</div>
</nav>
</div>
</div>
get '/about' => "song#about"
get '/song' => "song#song"
$ touch app/views/song/song.html.erb
$ touch app/views/song/song.html.erb
def song

end
def aboutend
@word = Word.all.sample
@word = Word.all.sample.entry
tracks = RSpotify::Track.search(@word)
@track = tracks.sample
<%= render 'shared/nav' %>

<div class="container">
<div class="row">
<div class="col-xs-12">
<h4 id="seed-word">Seed word: <%= @word %> </h4>
<h3 id="listening-info"> You are listening to
<%= @track.name %> from the album <%= @track.album.name %> by
<%= @track.artists.each.map{|x| x.name}.to_sentence %>.
<audio controls autoplay>
<source src="<%= @track.preview_url %>" type="audio/mpeg">
Sorry, your browser does not support embedded audio playback.
</audio>
</h3>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<h3 class="center"><%= link_to "Get another song.", song_path, id: "play-again-link" %></h3>
</div>
</div>
</div>

    Matt Hinea

    Written by

    Web Developer

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade