Introduction to CRUD in Rails
What's going on guys? John Elder here from codemy.com and in this video, we're going to set up our CRUD database stuff for our friends app with Ruby on Rails.
Alright guys, like I said, in this video we're going to work on the CRUD, the database stuff for our friends app with Ruby on Rails. But before we get started, if you like this video, you want to see more like it, be sure to smash the like button below, subscribe to the channel, give me a thumbs up for the YouTube algorithm, and check out codemy.com where I've dozens of courses with hundreds of videos that teach you to code. Use coupon code YOUTUBE1 to get $30 off membership. That's all my courses, videos, and books for one-time fee is just $49, which is insanely cheap.
Okay, in the last video, we set up sort of the structure of the website itself, knocked out a couple of web pages. In this video, we're going to start to set up all of this CRUD database stuff that allows us to add friends, view friends, edit friends, show friends. So if we click on one of these, you know, it shows the thing. We're going to set up all the database stuff that takes care of this, all the forms that take care of this. We're going to do it all in this video, and you're going to be absolutely amazed how easy this is. This is literally going to take like a minute to set this all up. Now, it'll take longer than that to make it look nice like this, but just to get the functionality of it, it's going to take no time at all. And that's the selling point of Ruby on Rails. It makes CRUD incredibly easy, and it just does it all for you automatically.
So you can see this is basically what we're going to end up with. We've got this, you know, page that lists all of our friends. If we want to look at one of them, we can click on it. It shows their name, their email address, their phone number, their Twitter handle, whatever you want. We can add to the database, but this is what I've chosen, which is just we can edit it if we want, change this to Steph Smith 1, update it. When we do, we get these little things here. We'll probably look at this in the next video, but you get the idea here. We can click here, we can navigate around, we can add friends, and that's what we're all going to do in this video.
Generating a Scaffold
So like I said, Rails makes this super easy, and let's just go ahead and do this. So let's head over to our code. And actually, we don't have to do anything in here just yet. So let's head over to the terminal and we're in our c/rails/friends/friends directory. I've got the master branch thing showing here.
So in order to create CRUD database stuff—and I keep calling it CRUD, if you're not familiar, CRUD stands for Create, Read, Update, and Delete or Destroy—and it's sort of the functionality most websites look for. Like think about like Facebook, you're creating a Facebook post, you are reading somebody else's post, you're updating your post, or you're deleting your post. CRUD. Every major website, Instagram, you're creating an Instagram post, you're reading somebody else's, you're updating yours, or you're deleting yours. Every major website works off the basis of CRUD. So that's what we're going to do here.
So to create CRUD in Rails, we use something called a scaffold. And we've looked at generators in the past when we generated our first web page. We use the rails g command, short for generate. Well, now we're going to rails g, we're going to use the generator and we want to create a scaffold. And a scaffold is just what it sounds like. When you're building a skyscraper, there's always a scaffold around it that the workers stand on to build the thing up. That's what this is. It's going to just create everything for us, and Rails calls it a scaffold.
So rails g scaffold. Now we're creating a model here, a database model, right? Or if you want to think more specifically, we're creating a table, a database table, right? So we need to name this, and I'm going to name this friends. So now we just need to decide what we want in our database table. If we head back over to the website and look, we've got here first name, last name, email, phone, and Twitter. So I think that's a good start. You can have as many things as you want, but that's that's good enough for us here. So let's head back over here, and let's go first_name. Now you have to put a colon and determine the data type that you want to use, and I want to use a string. This is a string of text, right? There are a bunch of different data types. I'll talk about those in just a minute. But, you know, strings are basic, you know, text. If you're going to use numbers, maybe you would use integers or floats or something like that. We'll take a look at these in just a minute, but now let's just knock this all out. So you separate each one with a space. So let's go last_name, and that's going to be a string too. And then what else did we have? Email, phone, and Twitter. So we could go email and that's a string, phone and that's a string, and twitter and that's a string. Notice it's the thing, colon, and then the data type. And again, we'll talk about data types in just a second. So now we can hit enter and Rails will pop up, do its thing, and done.
Reviewing Generated Files and Data Types
So it says the model name friends was recognized as plural, using friend instead. It will call it friend. Later on, we'll use friends in different ways. There's a whole thing about Rails and singular versus plural. We'll talk about that later. We don't really care about that right now, but just sort of remember there's something to learn there about the models being named singularly versus plural. You can override it if you want, but that's fine.
So right off the bat here, you can see it's created all kinds of stuff. A migration, a database migration. Whenever you're creating database stuff in almost any web development platform, you always create a database migration, and then you push that migration into the database. So it's created a migration for us. We'll have to push it into the database next, but we'll do that in just a second. It's created some testing stuff we don't care about that. It's created a bunch of routes for us because we're going to have a bunch of different pages: a page to show all our friends, a page to add a new friend, a page to edit a friend, you know, things like that, a page to delete a friend. And all of those pages need routes. It has created all those routes for us, and we'll look at that in a second. It's created controllers, and it's created a bunch of web pages. We can see in app/views/friends, an index page, an edit page, show page, a new page, and a form partial, looks like. See the underscore there designates a partial we learned in the last video. A bunch of more test stuff, some helpers, all kinds of stuff, some CSS stuff. Now this may conflict with our Bootstrap CSS, so we may have to delete this file. We'll take a look at that in a second, and we're ready to go.
If we head back over to our code really quick, and let's go to the db thing, you can see now there's a migration that's been created, and you can see first_name, last_name, email, phone, and twitter, and these are all strings. So very, very cool. So while we're thinking about these strings, let's talk very quickly about data types. There's a bunch of different data types you could use. This is all text, so we use strings. If you had numbers or something or a date, you would use a different data type. So if we head back over to this website, and I've pulled up this this page here at guides.rubyonrails.org/v3.2/migrations.html#supported-types. So this is a list of the data types that you can use. So we can, you see here's string, right? You could also use text, time, timestamp, integer, float, decimal, datetime, date, booleans, and binaries. And I'm not going to go into all these things because most of the time you're going to use like string. text is for larger chunks of, you know, text, I guess, but still you might just use string anyway. Let's see, integers you might use, floats, decimals you might use, datetime you might use. But again, we're not going to get into this. You can read all about these. This is the Rails guide page for migrations. You can just Google it if you want, and there's all kinds of stuff in here about what we just did, creating a migration. So okay, let's head back over here.
Running the Database Migration
And we're not done yet. We've created all these things, we've created a migration, and if we head back to our code really quickly, you'll see in this database folder there's a migration, but there's no actual schema. And the schema is the sort of finished product that's been pushed into the database. So we've created the first step here. Now we need to sort of make it live, make it actually work by pushing the migration into the database. And to do that, super simple. We just come down here and let's leave this up. We just go rails db:migrate. We just want to migrate that migration, we want to push that migration into the database and make it sort of live, right? And boom, it's done.
So now we can head back over to our code, and you can see now there's a schema file in our database folder. And if we click on this, well, there's all kinds of comments that we can get rid of. Uh, you can see here is our active record schema. We've created a table called friends, right? Even though it renamed it to friend, in the schema it calls it friends, so that's why I just always do it plural. It ends up being plural anyway. Even if you typed in friend, it would have pluralized it to friends here. We've got strings: first name, last name, email, phone, Twitter. We've got a datetime created_at and an updated_at thing that's been done for us, and we're ready to go. So we can close this. We don't really ever need to get into this database folder usually, but it's interesting just to look at.
Fixing the CSS Conflict
So if we head back over now to our terminal and let's go rails s to run our server. But keep in mind these things: app/views/friends, index, edit, show, new. We need to, we want to now look at those pages. So if we head back over here and go to our app directory and our views, you see now we have this friends directory that's been created. And inside of here, there's all kinds of stuff. There's an index page, a new, show, etc. And we can access these things by heading back over here. And let's just run this to make sure this is working. It is, and you'll notice right off the bat, everything looks different. There's space around here because when we created this scaffold, remember it created a CSS file for us. And now that CSS file is conflicting with Bootstrap, so we don't like that at all. So let's head back over here to our code and look in app/assets/stylesheets, and here you see this scaffold stylesheet, right? That's just been created, and this is the CSS code that's causing all the trouble, right? So we can just, if we want, delete this file, or if you want to rename it, maybe let's try renaming it, see if that works first. We used to be able to just rename it, but we might have to delete it. So, let's hit reload. Yeah, so it's still being called. So we need to actually delete it. So we can just come over here, right click and delete this guy. And now we can come back over to our website and just hit reload, and boom, it goes back to normal. So okay, that's working.
Testing the Full CRUD Functionality
So now if we go to our /friends directory, we see this is the friends page, right? If we click New Friend, we can add a new friend. Now, these don't look good. We'll change that. We'll bootstrapify them later, but this should work. So let's type in John Elder, [email protected]. My phone is 111-222-3333. My Twitter is @flatplanet, I think. I don't think the world is flat, but I find it hilarious that some people do, so that's my Twitter handle. Create Friend. Friend was successfully created. John Elder. We can go back, and now on our friends page, John Elder is listed. We can show him, we can edit him, we can destroy him if we want, we only, we won't yet. So let's add a new one. Uh, let's go Bob Fakename. He's at [email protected]. His phone is 333-4444. And he's at @bob. Create him. Go back. Now these are both listed, and this is fully functional. Now this just works. So we can, if we want to destroy him, we could. We can create a new friend, just do nonsense. We can go back, now this is listed. If we want to destroy, are you sure? And it works, and it was just that easy. Two commands we had to enter: the scaffold command, and then the migration, the push to migration command. And we now have a fully functioning CRUD database that can create, read, update, and delete all automatically. All the database stuff is done for us. This is just amazing how fast and easy this is. Now obviously, we want to tinker with this to make it look nicer, and we'll do that probably in the next video, but man, that's just amazing how fast this is, and this is why we love Rails.
Adding Navigation Links
So let's really quickly add this friends list to our nav bar. And to do that, I'm going to head back over here and break out of the server. And now let's rails routes to look at the routes that we have. And while this is doing its thing, let's look back over here at our config directory. So let's go config and then routes, and you'll notice here are the first two routes we created manually. It's created this one, the scaffolding has. And this is all it is: resources :friends, right? This is a way to sort of automate things for CRUD. Rails knows, hey, when we're calling resources and then this friends table in our database, create all the CRUD stuff for it, all the CRUD routes for it, and it will do it automatically. So we don't have, you know, 10 different routes that look like this. We just have this one that handles the show, the friends page, update friends, add friends, delete friends. It handles it all as a resource, and that's very cool.
So heading back over to the terminal, we can come up here and look and see where our routes are. So remember to get a link to URL, link, we take this, the prefix, and then put a _path behind it. So new_friend_path, edit_friend_path, uh friends_path, right? Let's head back over here and make a quick link to our nav bar. So maybe we'll do a couple of links. So let's head over to our app, we can close this assets thing. So we want app, header, here's our header navbar code. And just right next to the about us thing, I'll just copy this whole li section here. And we want, let's say, friends_path. Let's go Show Friends, or just Friends. So save this, head back over here, let's run our server. Our terminal. And remember this new_friend, right, because we're going to also put a link up there to add a friend. So this will be new_friend_path. But for now, I want to just make sure this worked. Oh, misspelled friends, of course I did. I always do that, seems like. f-r-i-e-n-d-s, okay. So friends_path. Now if we reload, all right. So we can go About Us and then Friends, list all our friends. All right, so that worked. And now remember we also want to make one for new friend. So let's come down here and grab this and paste another one. And let's go, Add a Friend, probably want a space there. And this is new_friend_path. Save this, head back over here, reload. If we want to add a friend, boom, that worked.
Exploring the Generated View Code
So we can change these pages by changing the code in our friends directory. So we're in app/views/friends, and this is self-evident. This is the index page, so we could change this to Friend List if we wanted to, right? If we save this, head back over here, hit reload, go to friends, now it says Friend List. For Add Friend, we could say Add New Friend or whatever we want, you know. So we go to the new page, and here it says Add New Friend. Save this, come back, hit reload. Add New Friend.
Now you'll notice this is kind of interesting. We'll get into this in the next video when we bootstrapify these things. This Add New Friend, all it has is this render tag. It's pulling a partial, right? It's pulling this form partial. If we look through here, let's see, there is a partial _form, right? And this will contain all of the form stuff, right? What's interesting is if we want to edit, it's calling that same form. If you know, we want to create a new, same form. Show doesn't have a form, I don't think, but this one form is doing the work on a couple of different pages. So that's kind of interesting, and we'll get into what that's all about in the next couple of videos.
Recap and Final Thoughts
So very, very cool and very easy to create a fully functioning CRUD application that just works. Now obviously, like I said, this is not great looking, but you know, who cares? We can fix that really easily. That's just a design thing. It's the functionality that's the important thing, and man, it took us 30 seconds to knock out the functionality for all of this CRUD stuff. Believe me, back in the day before things like Ruby on Rails came around, to create that code, the database code to handle all this stuff would have taken me two weeks. I mean it just would have. And we just did it in 30 seconds. That is just incredible, and it still blows me away to this day, even though I've been using Rails for 10 or 15 years or whatever, almost since it came out. Uh, it's still every time I do this, I'm just like, wow, that was fast. And uh, very, very cool.
So like I said, now that you know how to do CRUD with Rails, you can make any kind of website you want. Create, Read, Update, and Delete, that's all there is to it, and super easy with Ruby on Rails. So I'm super excited about this video. I don't know why, this just geeks me out. I love this stuff, and I love how fast and easy it is with Rails. Django works pretty good like, but even Django isn't quite that fast for knocking out a CRUD application, and you still have to tinker with some things and some routes and some stuff behind the scenes. Rails just takes care of it all right out of the box, super fast, super easy, and super cool. So that's all for this video. If you liked it, be sure to smash the like button below, subscribe to the channel, give me a thumbs up for the YouTube algorithm, and check out codemy.com where you can use coupon code YOUTUBE1 to get $30 off membership. So I paid just $49 to access all my courses, over 45 courses, hundreds of videos in the PDFs of all my bestselling coding books, including this Ruby on Rails coding book that is super cool. Join over 100,000 students learning to code just like you. My name is John Elder from codemy.com and I'll see you in the next video.