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_trackerusingPostGreSQLfor your database. - Don't forget to run
rake db:createto actually create the database. And make sure PostGres is running! cdinto your app, and runrails s, and checklocalhost:3000just 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
Trackmodel, that will do exactly this. - At the very least, our
Trackmodel 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 Trackfollowed by the attributes you'd like to add. You can also add those later manually in the migration file. (you can also userails g resourceif 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 consoleand 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
rootpath. In yourroutes.rbfile, type in something likeroot 'tracks#index'. This will tells rails that the root of our app lives in thetrackscontroller, and look for theindexaction. - Next we'll need to actually set up this
trackscontroller we defined in step 2. Userails generate controllerfollowed by the name, which in this case istracks. - Jump into your
app/controllers/tracks_controller.rbfile and create a method calledindexthat renders an index.html.erb file. - Now we'll need some views. Create a new directory in your
app/viewscalledtracks(if it wasn't already created..), and inside that create a file calledindex.html.erb. Add anh1element or something that says "This works!". - Start up your server and check
localhost:3000and make sure it's all working. - ALRIGHT. Set up all the routes you'll need in your
routes.rbfile that will be necessary for all your CRUD operations. A nice fancy shortcut is using a helper in Rails calledresources. Above ourrootroute, 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
indexmethod in yourtrackscontroller, set an instance variable called something like@tracks, and have it point toTrack.all. In other words, all the tracks. You can now use@trackswithin yourindex.html.erbfile, 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
trackscontroller 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.