AM Coder - Ruby on Rails/Postgres Full Crud Api from 0 to Deployed in 15 Minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody this is alex merced from alexmercedcoder.com and in this video we're going to do is i'm going to try to quickly create a ruby on rails api um make sure it works and then deploy it to heroku all in one video and i think we might be able to get this done in 15-20 minutes let's see if we can do it okay so first let me so that way you guys can compare with what you got these are the versions of the things i'll be using that i have so rails dash v so that's the ruby on rails uh library the framework of ruby so i have 2.70 for ruby um bundler fee and gem dash v so that's jim that's the package manager for ruby okay cool so now you know those things okay so now we're gonna do is gonna create a new rails project okay and basically we're gonna do is do a rails new we're gonna call this rails new cats cats api cause we're gonna do cats okay and i'm gonna do a couple flags so first i'm gonna do a database flying because i want to use postgres and this just saves me time postgresql okay so the id the point of that flag is to it'll automatically configure it from the default sql light settings to and then hit dash api this configures it so that way all the extra stuff for like views is not there so that way it's just for a setup for an api which is a little bit quicker to set up so we do this cool oh okay we're all in we're all set up okay so now cats api is right there so i'm going to go cd into that folder cats api okay good good good now let me just double check the database settings make sure that's set up the way it should be set up db actually that would be inside config uh database.yaml so let's take a look good it's set up for postgres okay now for the production database i want it set up with this setting so i'm going to delete all this because heroku's just going to have a url um that it's going to fit in there so there we go that's set that's good to go uh and then for my local environment okay let me set the settings up where is it development tests that's api test that's all fine but i want to set up the encoding pool okay should be trying to connect to my local in order to specify that so i'm going to do that here um development host should be localhost port should be five four three two for postgres and username or you take his username i have tests actually might be user test password test this is just my local test user and then everybody says that database cats api development so that's fine with me and i'm just gonna add these three to the testing one just in case i get confused which one's running when i'm working locally okay and i think that is all i need okie dokie so we'll leave that as that so that's the main thing i wanted to make sure i checked the gym file should be good nothing i have to change there let's see their pg is already set up and sqlite it's not there so that's good to go um yeah so the next thing all we have to do is just do this a rails g for generate scaffold cat okay so we're going to create a thing a model called cat okay and actually uppercase cat or i think just send me the lowercase cat and then we just put in a couple fields so cats will have to give a really simple they'll just have a name okay and nothing else so let's hit enter okay and that's going to take a second and there it goes it's scaffold everything so see everything you need for a cad api is set so if i were to go to config slash routes okay see all the routes for cats are set up okay if i go to app and i go to con models there is the cat model right there not much to it okay and then if i go to controllers and there's the cats controller and see all the crud routes are all created for me so it's all set to go uh the only thing else i want to check is migration so here's the migrate folder and then this should be the file that says to migrate cats and that's where that's exactly what it says so see it's going to migrate cats with the name field see all that from one command isn't that nice so now the only thing i have to do is make sure that my database gets sets that up so now i just run uh i think it's rails [Music] uh or no actually i think it's rake rake db migrate i think is the command and that's gonna migrate the database oh i didn't quite go the way we wanted it oh we have to create db okay so first it's a rake db i think it's create okay let's see what it said for that one i think right here authentication failed for user test can create pass api notification one of the settings isn't 100 right five four three two five four three two test test and it should be the password i am just going to use the test database because that exists oh okay i'll change it again over here test save let's try rake bb migrate again that's it so there we go so see the cats table was created i can confirm that by just logging into my local postgres psql and then what i want to do is connect to the test database okay and then if i do slash dt and there is the cat table okay so back in my terminal uh so now this should be working let's give it a shot rails server okay so the server is running on port 3000 on port 3000 so let's go to the let's open up postman uh over here postman so first let's go create a cat so localhost three thousand slash actually i should check what the rats are gonna be i think it should just be cat we'll find out it's gonna be cat or cats cat that's would be a post and the body will just be have a name so let's hit send 404 not found okay so maybe it is cats [Music] okay something wrong with my oh there's a random comma in my json okay there we go let's do it again and there we go we made a cat okay let's make another cat snowball cool now let's do a get on cats okay so that's working okay and then if i want to update a cat so we'll say i want to upgrade update howdy howdy 2. we'll just change them to howdy the third and uh oh i need to change that to a put put so that was just the show route uh but good that works um let's try that again what and see now we changed it to howdy3 and then let's delete howdy cool and now if i go like this and then do a get see how he's gone okay so the api works so we are cooking with fire okay save okay now man i'm not in a rush to go push this to github because it's just a practice project so instead of connect instead of pushing this particular project to github and then connecting my github to heroku which is one fine way to uh what i'm going to do is i'm going to connect to heroku directly so how do i do that okay so what i can do is i just go to create a new application here on um all right yeah i right here on heroku we're actually going to show you even the easier way assuming you already have the heroku cli installed all you have to do is type in heroku create so long as you've already logged in to heroku on your cli so how do you know i can type in heroku login and it'll let me log in and i'll go through this whole process and then you just type in heroku create now if you do this what it's going to do is that instead of you pushing the github and then github updating heroku you're pushing to heroku directly so that means when you push to github is separate then when you push to heroku it's going to create a new remote so it's going to create a project so heroku create so see it's creating a project and now if i go to git remote i see there's a new remote on this project called heroku so now i can do a get at all let's commit everything and when you create a rails project it automatically makes a git repo inside the folder keep that in mind git commit dash m api done and then we push that to heroku get push heroku master not origin master roku master that's gonna push it to heroku and then i can watch the whole process of it you know we pushed it's gonna fail okay fail to install gyms we have well let's see here oh okay so we have to fix something on the gem file i'll form actually if malform shabang on [Music] what line it's in the [Music] bin bundle that's where it says okay so it says right there bin bundle has a malfunction bank so i'm gonna go inside the folder there's this file called bundle and the issue is that it should just say ruby so i'm gonna delete the 2.7 and that should fix so then i do a new commit get add git commit dash m bundle fix not origin pushing it to heroku master okay so see now it's doing all the stuff it's installing all the gems okay this takes a second technically after this it'll be deployed the problem is we haven't migrated our new online database like this should pick up this will should realize that we need postgres because it's going to notice that inside the uh the gem files that we installed the postgres gem and based on that it's actually going to automatically provision a postgres database on heroku so we won't have to do anything okay and then because we use that url environmental variable in the database that was kind of already there so again i'll bring that up again config config database.yaml because we're using this right here for the database url for production heroku is going to feed that environmental variable in there automatically when it provisions a database so all i got to do now is i need to migrate the database on heroku so i need to do heroku um run which means allows me to run a command on the heroku server so now i need to run that same command rake db migrate okay the db should already be created by heroku if not we'll create it so let's see here heroku run db migrate and i don't have to put an app flag this time because when you have the heroku remote connected to your project it then knows what app you're running the command for so i don't have to put that extra app flag now so it does that so it's running the command and let's see if we get the right response running up so okay that's that looks good to me uh yep so let's see here migration insert into skew integration yeah that all looks good okay so it looks like the database is migrated so i think we're good so let's go back over here i just want to get the url from heroku so here's the url of our heroku application which you can also get from the heroku dashboard but there it is copy i want to take that into postman slash cats okay we're gonna go first create a cat so yep and it works our api is deployed okay it's online it works okay and i don't have to really configure much this was all super easy we did it in literally 15 minutes i said we could do in 15 minutes and it could have been uh even a little bit faster um without all the explanations and whatnot just to kind of show you sort of how ruby on rails really allows you to develop very rapidly okay these sort of batteries included frameworks um because i didn't really have to do like almost any coding it just kind of all did itself i just had to put in some commands and deploy it okay but yeah it works so uh leave it that i'll see you guys later have a great day and enjoy
Info
Channel: Alex Merced - Full Stack Developer
Views: 1,645
Rating: 4.5 out of 5
Keywords: Programming, Javascript, PHP, Python, Go, Typescript
Id: n9V_1X8uGZM
Channel Id: undefined
Length: 15min 55sec (955 seconds)
Published: Thu Aug 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.