Today we're going to build a simple, single model CRUD application in Rails using PostGres. We're going to be building a music tracking app to store our favorite songs, and artists.
- Create a new directory somewhere on your computer, and whip up a new rails app called
music_tracker
usingPostGreSQL
for your database. - Don't forget to run
rake db:create
to actually create the database. And make sure PostGres is running! cd
into your app, and runrails s
, and checklocalhost:3000
just to make sure everything is working. Afterwards make sure to kill the server(ctrl c)
before moving on so we can do some work.
- Since our app will need to track our favorite tunes, we'll need a model to store this information. So let's create a
Track
model, that will do exactly this. - At the very least, our
Track
model will need anartist:text
, aname:text
,album:text
, and maybe agenre:text
. Feel free to expand on this. Create the model by runningrails generate model Track
followed by the attributes you'd like to add. You can also add those later manually in the migration file. (you can also userails g resource
if you're feeling fancy, which will create things for you automatically that we'll be setting up manually in the next few steps). - Set up any database level validations you think would be important.
- Finally run
rake db:migrate
. - I encourage you to test your model by using the rails console, and creating some new tracks manually, and saving them/deleting them from your database. Hop down into terminal and run
rails console
and run something likeTrack.create()
and create/delete some tracks.
- Alright routes time. Open up
config/routes.rb
. This is where all your routes will live. - Let's start with creating our
root
path. In yourroutes.rb
file, type in something likeroot 'tracks#index'
. This will tells rails that the root of our app lives in thetracks
controller, and look for theindex
action. - Next we'll need to actually set up this
tracks
controller we defined in step 2. Userails generate controller
followed by the name, which in this case istracks
. - Jump into your
app/controllers/tracks_controller.rb
file and create a method calledindex
that renders an index.html.erb file. - Now we'll need some views. Create a new directory in your
app/views
calledtracks
(if it wasn't already created..), and inside that create a file calledindex.html.erb
. Add anh1
element or something that says "This works!". - Start up your server and check
localhost:3000
and make sure it's all working. - ALRIGHT. Set up all the routes you'll need in your
routes.rb
file that will be necessary for all your CRUD operations. A nice fancy shortcut is using a helper in Rails calledresources
. Above ourroot
route, try usingresources :tracks
. In the terminal, runrake routes
, and look at all the lovely routes Rails has set up automatically for you. Now you just have to be mindful to follow the RESTful actions in your controller.
- Assumming you'll want to display your tracks to the page, you'll need a way to grab your tracks from the database within Rails. So inside your
index
method in yourtracks
controller, set an instance variable called something like@tracks
, and have it point toTrack.all
. In other words, all the tracks. You can now use@tracks
within yourindex.html.erb
file, and iterate over it printing your tracks to the dom.
SO COOL.
- For the following steps, I'm going to rely on you guys to do the heavy lifting. Flesh out your
tracks
controller methods to perform all the proper CRUD operations. Be mindful to follow RESTful api naming conventions as layed out by Rails assuming you usedresources
. To double check, you can always peek usingrake routes
. - You'll obviously also need to create views for these routes, that contain form elements so we can populate our DB. Stuff you're all familiar with having built a CRUD app in Node & Express. Don't forget about csrf-tokens with your forms!
- In your create action, you'll need to be mindful of
params.require()
and.permit()
.. so that you can pass all of the form values intoTrack.new
. - When you're finished, dig into styling (the best part, IMO...). Make it look real nice.
# For Kristyn Build a second model for user auth, migrate it over. Create a new views for session login + new controllers with corresponding actions.