Skip to content

Instantly share code, notes, and snippets.

@grdunn
Last active February 19, 2016 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grdunn/3afc6e27cf47e964cfdd to your computer and use it in GitHub Desktop.
Save grdunn/3afc6e27cf47e964cfdd to your computer and use it in GitHub Desktop.

Rails CRUD Application

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.

Step 1 Initialize

  1. Create a new directory somewhere on your computer, and whip up a new rails app called music_tracker using PostGreSQL for your database.
  2. Don't forget to run rake db:create to actually create the database. And make sure PostGres is running!
  3. cd into your app, and run rails s, and check localhost: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.

Step 2 Model

  1. 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.
  2. At the very least, our Track model will need an artist:text, a name:text, album:text, and maybe a genre:text. Feel free to expand on this. Create the model by running rails 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 use rails 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).
  3. Set up any database level validations you think would be important.
  4. Finally run rake db:migrate.
  5. 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 like Track.create() and create/delete some tracks.

Step 3 Routes & Controllers

  1. Alright routes time. Open up config/routes.rb. This is where all your routes will live.
  2. Let's start with creating our root path. In your routes.rb file, type in something like root 'tracks#index'. This will tells rails that the root of our app lives in the tracks controller, and look for the index action.
  3. Next we'll need to actually set up this tracks controller we defined in step 2. Use rails generate controller followed by the name, which in this case is tracks.
  4. Jump into your app/controllers/tracks_controller.rb file and create a method called index that renders an index.html.erb file.
  5. Now we'll need some views. Create a new directory in your app/views called tracks (if it wasn't already created..), and inside that create a file called index.html.erb. Add an h1 element or something that says "This works!".
  6. Start up your server and check localhost:3000 and make sure it's all working.
  7. 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 called resources. Above our root route, try using resources :tracks. In the terminal, run rake 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.

Step 4 R of CRUD.

  1. 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 your tracks controller, set an instance variable called something like @tracks, and have it point to Track.all. In other words, all the tracks. You can now use @tracks within your index.html.erb file, and iterate over it printing your tracks to the dom.

SO COOL.

Step 5 CUD-DLE ME. Create, Update & Delete.

  1. 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 used resources. To double check, you can always peek using rake routes.
  2. 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!
  3. 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 into Track.new.
  4. 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment