Ruby on Rails CRUD Application

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
of my world uh today i want to do something fun i always want to do something fun today what we're gonna do i'm gonna follow this tutorial uh this tutorial i'm gonna make an app today i'm gonna make an app so um a cd into my github folder i'm going to rails new we'll just call this cruddy called cruddy buddy and i want to say what do i want to say obviously to close some windows so i want to say uh i just don't know if i if i need two hyphens or one hyphen looks like one hyphen two hyphens that's my syntax i like to use postgres because the boot camp that i'm part of participating in is using postgres it's more robust more professional more heavy duty rails is a web application development framework written in the ruby programming language it's designed to make programming web applications easier by making assumptions about what every developer needs to get started it allows you to write less code while accomplishing more than many other languages and frameworks experienced rails developers and also replaceable systems makes developing more fun rails is designed to take the weight of events configurations up to bella's shoulder by including everything you know started a web application it also writes code in their accomplish more ruby versus on rails uh ruby is a language rails is a framework uh model view controller when you create the ruby app it creates the model to view in the controller it creates it all for you you can use the database uses sq lite by default but i'm using postgres with the models the views and the controllers convention over configuration do not repeat yourself a lot of benefits of using ruby on rails um generate the model for the application so this is gonna we're generating a model for dog it's going to have a name and a model and by default this will be a string and this will be a string this creates two files models dog.rb and migrate create dogs so it creates a migration when we generate rails generate model that gives us a dot rb file inside the models folder and it gives us a migration this will be fun i was taught to say to specify but it is string by default good cannot find a generator dog hmm interesting [Music] hmm what was the error couldn't find a generator dog oh rails generate a model there we go so we got app migrate we've got a migration inside the db migrate folder we got a model inside the app models folder we got test okay so we invoke activerecord and activerecord does this and then we invoke testunit and testunit does this dogtest.rb and dogs.yaml is inside fixtures i don't know what this stuff is but it's for testing testing so next step generate a controller generate a controller called dogs index show new edit those are going to be uh the methods i don't know invoke erb so it gave us some views we got a dog's view an index view for dogs well oh that created a folder dogs inside the dogs we got the index the show for displaying individual dog the new dog page for with a form to create a new dog and the edit page where we can edit an existing dog and we invoke test unit and they gave us a test inside the controllers folder invoke helper so inside app helpers we have dog helper rb invoke test unit assets scss so now we've got a style sheet for dogs so we can look at all these files here i don't know how to zoom in and add them i use vs code but vs code is not opening for me so command plus i tried i tried doing that earlier maybe i need to do command shift no it's not working increase font size only applies to the so yeah inside db we got our migration config so yeah we index show new edit we got index show new edit and what is inside those files control is in the controllers no it's in the dogs view app views dogs i don't like how it goes back and forth between the two my control tab through you know in my browser it just it just cycles through all the tabs but here if i want to go to another tab i have to so it's like i don't know what order they're in you know one two one two but if i'm at two and i wanna go to three i tab twice and if i'm at three i to go to four no tabbing twice didn't do it then hmm so if i tab once i have no idea which one it's going to take me to oh well it'll take me to the last one yeah and then if i tab twice it'll take me to the last one before that i don't know i think it's dumb i think it's dumb so we've got these got a h1 tag so okay routes config routes rb file also gets automatically updated config routes rb routes dot rails.application.routes.draw do get dogs index get dog show get dogs new get dogs edit since we are going to use all crud operations let's use the shortcut rails has provided us update the config routes as such what you want me to replace everything there with okay i think that's what they want me to do just going to comment it out so it's still there now we are ready to migrate our data double check our migration file to make sure it looks appropriate this is our migration we have a class create dogs and the class create dogs what does it do well it changes something it creates a dog it creates a table called dogs and it loops through this is a for loop or a while loop or a for each loop t string name so okay right create table is a is a rails method that creates an html table from from dogs so it loops through and dog number one it's dogs t so when t equals one or zero then this is dog number one and then so it prints out a string which is the name of the dog i'm not sure i don't know so that looks appropriate that's just what we have so i run rake db migrate this will create a table in our sqlite database or in my case postgres based on what is in our db migrate the file name was a time stamp and a create dogs 2021 january 15th 21 46 oh that must be greenwich mean time because it's not that's not what time it is that is it's it's one o'clock here and it's nine o'clock gmt apparently i would guess so let's add some dogs so we can run the tests so i have to run rake db migrate oh no right i have to create the database first maybe this is unique to postgres i had this error before and so uh rake db create now break db migrate there are some other options you can append to those commands okay so we're migrated so created the table in the database now we've created a table so we've when we ran this migration we what we did rake db migrate that goes through and it says do we have any migrations that have not been run yet and so this one hasn't been run yet so it went through and it ran all this code it said create a table in the database and then this is our schema so it updated this file so see we have create table dogs so db roll back oh so i got that apostrophe on the end of rollback and that screwed me up and so i was stuck in this thing so i hit ctrl c ctrl c so i'm going to undo that uh migration we're going to undo the running event migration reverted so it should be different now in the schema right schema.rb has nothing in it now enable extension so this is plp gsql that's postgres and postgres has its own command line called plp or something like plpg and so uh there that's the change changed our schema so so we have a table in the database now we want to add some dogs okay so we can we can do this add some dogs to the seeds file dog.create and so this is ruby dog.create we already have a class dogs somewhere in the models when we made the dogs model the dog.rb class dog it's totally empty class but i would think it would have a name and a motto in the class but um some dogs views so so we throw that in the seeds dot rb file rake dbc to add the dogs to the database let's see what we got see what we got so they're in the database now this should be uh in order to check if the dogs have been added to your database run rails console or rail c so let's go into the console rails console this is my other app so we're in the rails console now and what do we want to run we want to say uh rail c got all you should see that four dogs have been added yeah let's try that dog.all dog load 5.5 milliseconds select dogs from dogs limit one dollar limit eleven hashtag active activerecord relation i don't see any dogs in here uh okay you should see that the four dogs have been added i didn't see any dogs fyi run rails routes or go to local host 3000 rails info routes to see all routes your application is configured to i need to be ctrl c to exit the console uh maybe that maybe that's wrong command t gives me a new tab and i'm in rails routes we've got a lot of routes here uh okay oh man there's a lot of uh rails postmark inbound emails rails relay inbound emails send grid inbound emails mandrel mandrel male gun conductor so these are get post ingresses rails conductor action mailbox inbound emails index create new edit show update update destroy inbound emails what is this stuff what is action mailbox action mailbox routes incoming emails to controller like mailboxes for processing and rails it ships with ingresses for mail gun mandrel postmark send grid you can also handle inbound mails directly via the built-in exim postfix and mail ingresses the inbound emails are turned into inbound email records using active record and a feature lifestyle tracking storage of the original email on cloud storage via active storage and responsible data handling by on by uh with on by default incineration these inbound emails are routed asynchronously to you using active job to one or several dedicated mailboxes which are capable of interacting directly with the rest of your domain model setup install migrations needed for inbound email and secure active storage is set up action mailbox install dv migrate yeah i don't know what that is so uh let's try this one we'll probably have to run uh the server yeah we've got all our routes here a little bit prettier than the terminal output so here we have dogs slash dogs slash index slash dog slash show slash dog slash news dog slash exit their edit so we do slash dogs oh what about dog slash show okay that looks awful a lot like our dogs show okay oh i don't have auto save turned on oh that's why the seeds thing didn't work okay okay let's go back have everything saved what was that command here we're in the server just do a new one so uh rails i okay now we've got some dogs this is where the dogs printed out before no dogs here's the same place hashtag dog id one name nala model born to be wild created at updated at there's dog number one hashtag angle bracket dog number two we have dog number two hashtag angle bracket dog number three right there hashtag angle bracket dog number four those are some long dogs long there's a angle bracket here closing this angle brackets we've got these hashtags hashtag so we've got the dogs they've got all their attributes and it's kind of a key value pair type of deal but these are columns in a database the dog id column and it columns in a table in a database name column model column created at updated ad so good we're uh we're doing good start with reed so let's go into the dogs controller we have functions index show new edit these are the ones that we defined when we rails g controller so in the index function we want to say this you so in the index we have this embedded ruby tag it's like an html tag except it's going to execute there's ruby code inside and it's going to do this loop so we're going to loop through all the dogs at dogs is referring to the dogs that are in the dog's table in the database we're going to do loop through each and do and we're going to feed dog past dog into the loop so dog is always gonna refer to uh this whichever dog that we're on so we'll go through dog one pass it in and then we're talking about dog one we do get did we're on dog dog two now we pass it in to the pipes and now this is dog two dog two and then dog three and pass it in dog three dog three so we have a regular html tag and then some more ruby code and the equal sign i forgot link two so it creates a link this is a rails method dog.name so that's the the name of the dog is the text of the link and then dog path dog okay so each dog has its own page and then so the page for each dog has its own path and so rails says dog path is the path to that specific dog okay we've got all our dogs here and we can go to each one you know and so check this out uh so here in the code we've got dog.name inside these so i can take this go inside the uh the view notice when i which one was it which file did i go in and i wrote hi i am a dog so in vs code i can hit command shift f and now i'm searching all the files for this phrase find in project the whole project so i'm searching for hi i am here in app views dog show in the show page hi i am a dog and my name is we don't want to link i just want dog name so can i do this save that so yeah through an error dogs.show dog so maybe i need to say like dog.1 or dog one let's try that nope how about nope how about that's not gonna work how about uh i give up i'm just gonna delete all this okay we are there's no backlink show details of a specific dog controller in the show method appcontroller dogs controller.rb so we want to put this in our show method show dogs controller show so we're gonna show uh dog equals dog dot find params id huh what if i take this dark side show ah this is a hashtag or a a hash a hayesh this is a hash that corresponds to whatever dog dog number one it doesn't tell us what dog number one is but this does so i don't know how to translate this into but we just passed dog number one here dog dot find params id so we pass the id parameter into the dog dot find one yeah we've got the dog id so anyway you so so we got the show method show i want to copy paste here so we have a link to remove uh dog this refers to the page the remove page and it's going to call the method delete and it will confirm it'll give us a message confirm is a method create read update delete create a new dog app controllers dog controller new and create methods so in the dog controller we have to say def dogs perhaps we have a new function and a create function um okay so the new function and then a create function and then a private method dog params this is all in the show dogs controller dogs controller so after a new create private okay you you you we've got an index index show show new new create create edit [Music] update destroy and a dog params all right as we want to refactor the controller file that sounds boring i don't want to refactor the controller file uh let's refactor sometimes you got a refactor so what's it gonna look like we got uh before action right so this is gonna we're gonna take out some repetition and then we're gonna add some other things so that we don't need the repetition so div show needs to be empty my show function so but show is mentioned up here and so i guess we're going to be inserting some functionality into this method via this statement here i suppose so we've got deaf new at dog new still got that you know what i should have done i like here using command slash that comments the whole line def create good good edit update same destroy gets a little shorter all right because we're we're getting rid of this dog.find params id so that needs to go from the update the update method and the destroy method and then create method did i miss that no no it's just dog.find it's repetitive so we're gonna so update update destroy destroy then we have dog params and current dog so current dog is the new thing so we say before action current dog we're going to run current dog so i guess somehow this line of code is going to insert this code into these functions which is cool i don't know why you wouldn't just say you know make another function and just stick that function into the other functions but so they're totally refactored we have refactored it looks better we're going to add some more links so i just was curious do i have a server running uh this is my console this is the server it's still running let's see if my app is uh this is really ugly we've got nala she's born to be wild we can remove her alex calm as can be leroy so i don't have a way of making a new dog and i have to use the back arrow to get back which is not good that's more links to our index to connect all the other pages in one area okay that's what that's what i was hoping for so index.erb let's see what that did all right um kind of ugly but understanding exactly what rails is doing helps you know where to debug here's what happens when you click a button on your form the browser requends it sends a request to the server rails gets the request and follows what method was listed get post delete etc and tries to match it with the appropriate route once it finds the correct route it maps to the appropriate method in your controller and calls on that from there it finds the associated view page index edit show new with that method to display the results to the user don't be afraid to use buy bug that's focus that guy is focused maybe if i put on a tie every day i'd be more productive i should put on a tie bye bug what is by bug bye bug well what i want to do right now you say i want to say get in it then i'm going to say uh shoot so i'm going to github go to github make a new repository call it cruddy buddy with an underscore create my public no read me no get ignored i'm going to admit skip that commit hyphen m so we've committed these files and then uh specify get branch main [Music] oh maybe i need master i'm gonna type it i'm gonna type it because i'm still learning uh master maybe not okay well let's just add this remote origin and then we'll try to do get push hyphen you origin main get push hyphen new origin main oops failed to push some refs i've gotten this before i don't know what it means i failed to push some refs i followed the instructions correctly but then when i got to push to get this error the repo was up to date beforehand rejected master master fresh first error failed to push some refs too hint updates were rejected because the remote contains work that you do not uh not have locally this is usually caused by another repository pushed into the same ref you may want to first integrate in remote changes get pull before pushing again note fast forward in the get push help dude if you are sure methods instructions uh error message uh did you follow the instructions exactly first clone a fresh uh copy of your repo using the hyphen hyphen mirror flag get clone hyphen hyphen it's not what i want to do this is not what i want this branch main what does this even do branch management here we go right from the horse's mouth now that you've created merged and deleted some branches let's look at some branch management tools that will come in handy when you begin using branches all the time the get branch command does more than just create and delete branches if you run it with no arguments you'll get a simple listing of your current branches git branch iss 53 master testing notice the asterisk character that prefixes the master branch it indicates the branch that you currently have checked out i.e the branch that head points to this means that if you commit to this point the master branch will be moved forward to your new work to see the last commit on each branch you can run git branch hyphen v interesting uh get branch hyphy iss 53 93b 142 x fixed javascript issue merge branch at scott the author known for read me zeus hyphen hyper merged and hyphen hybrid no hyphen merge options can filter this list of branches that you have or have not yet merged into the brass you currently on to see which branches emerged in the branch on you can run get branch merge to see which branches are already merged into the branch you're on because you already merged uh you see the list branches without the asterisk in front of them generally fine to delete the git branch you already incorporate they're working the see all branches contain your version uh if you see a other branch contains uh you really want to delete the branch you lose your work on and force it with the what does git branch mean the get branch commands primary functions are to create lists rename and delete branches to operate further on the resulting branches the command is only commonly commonly used for the command the get branch commands primary functions are to create list rename and delete branches yeah so i want to i'm trying to create a new branch there are no branches and i want to create one i want to create a new branch called master or main but refname refs heads mastered not found fatal branch rename failed how to list all current branches uh to see local branches run this command get branch there are no branches i love it when i hit command tab and it does nothing oh i'm tabbing into webstorm i'm going to exit this program to see remote branches run this nothing to see all local and remote branches run this command get branch hyphen nothing so how to make a new branch i mean i know it's that command i was trying to run and that command's not working and so i don't understand let's go to a simple example of branching merging with the workflow you might use in the real order fallacy steps do works on the website create a branch for a new story you're working on and uh deuce yeah you create you got a user store you want to implement that user story you want to make it come to life you want to make that story come true so you create a new branch so you can work just on that one feature this is genius this is genius this is what i'm learning do some work on that branch at this stage you'll receive a call another issue is critical you needed a hot fix you do the following switch to production branch create a branch on the hot fix after system or it's a hotbed branch but it's no production switch back to original user story continue working basic branching let's first say working on a project you have a couple commits read on the master branch master c1 c2 c0 figure 18 a simple commit history you've decided you're going back to work on issue 53 and whatever issue tracking system your company uses tray to create a new branch and switch it to a time and you run the git checkout command with the hyphen b switch the shorthand for branch uh creating creating a new branch pointer you work on your website and do some commits doing so moves the iss 53 branch forward because you have it checked out that is your head is pointing to it vim index html git commit hyphen a hyphen m create new footer issue 53 master iss 53 figure figure now you get the call that there is an issue with the website and you need to fix it immediately with git you don't have to deploy your fix along with iss changes you've made and you don't have to put a lot of effort into reverting those changes before you can work on applying your fix get checkout master switch to branch master at this point blah blah blah blah all blah um why can't i push why can't i push what happened when i tried to put i did push search respect main does not match any what is ref spec a ref spec maps a branch in the local repository to a branch in the remote repository yeah so what does it say refspec main does not match any so i over here in github yeah it wants me to use main but when i say get branch hyphen capital main error ref name refs heads master not found hmm this is disturbing well i'm going to conclude this i think we've been at it for a while and i just want to thank you for joining me bye
Info
Channel: Aaron Dull
Views: 291
Rating: 5 out of 5
Keywords:
Id: YLFkNfytHgs
Channel Id: undefined
Length: 76min 26sec (4586 seconds)
Published: Fri Jan 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.