Ruby on Rails Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
well hello internet and welcome to part one on my Ruby on Rails tutorial over the course of the next couple videos I'm going to completely cover the rails web framework and basically I decided to cover rails because it's really a absolutely amazing framework after you get over the basic differences between developing rails applications versus other applications you may be used to you're really going to be able to embrace rails and what's great about it is it's going to provide pre-written software functionality for creating web applications it's going to in general make it very easy to create common tools used in web development for database access templating pages authentication all the things that are normally complicated are very easy in rails and on top of that it was written using the programming language Ruby which is extremely easy for newcomers to development in general to grasp so basically how do we install rails well first off you're going to need to go get my SQL because that's the database I chose to use here because it's very common and then we're going to have to go to install rails com all right so here we are and install rails comm and this is the easiest way to install everything just click on start now then you can pick whatever operating system you use so if you use Windows use Windows click on that and it's going to walk you step-by-step through the entire process of installing as well as having a little trouble shooting button down here everything is right there and of course it's great because it's going to cover for Mac Windows as well as Ubuntu or Linux installations of rails so walk your way through that and then come back to this tutorial and we'll start writing some code alright so basically rails uses two main concepts that you need to grasp and like I said it's a little bit hard to start writing rails applications but after you get over the initial hump it gets very easy one thing you need to know is rails follows two different premises one is that you should not repeat yourself or don't repeat yourself and this just means that we need to avoid duplicating our code which is going to make our web application much easier to maintain on top of that another principle rails follows is convention over configuration now rails provides default ways of doing everything and what's great about this is it's going to minimize the amount of code you have to write to basically what you've personally decide that you want to customize about your web application another great thing about this is that rails defaults to follow all of the best practice is for web application development so you basically just let it run and don't worry about how it operates another thing to understand here is that rails is built around the MVC pattern which is Model View controller when somebody goes to a browser to use your web application they are going to be routed to one of many different controllers now the controller is going to respond to user events and help translate what is in the model or the database or whatever out into a viewable form on the browser using what we call the view and by doing things this way each time you change your view you don't have to change the model and vice versa and as you see some examples here it's going to become very clear while this is the way to develop a web application another thing to understand is the controller inside of rails is referred to as the action controller and the model is referred to as the active record and the routing part down here what's good basically going to happen is you're going to go to a browser and like most web applications you would go to a directory well inside of rails each thing that looks like a directory is actually going to be a different controller and there's going to be a routing process that goes on here between the browser and the controller port and then the view part of the rails application is going to be called the action view so you have the action controller the active record and the action view when we jump over and develop a rails application all right so here we are basically I have sublime text over here it's a nice text editor over here I'm going to have my browser where I'm going to load my application when it's ready nothing's there right now so it says no webpage is available and then down here I have my terminal or if you're on Windows you're going to have a command line and I have multiple tabs open so I'll be able to start the server and also be able to operate inside of your course you're going to want to create some directory where you want to be able to save everything I have mine in rails code clear that out and to develop a rails application you're basically just going to go inside of your terminal and you're going to type in rails new and I'm going to be creating over the next couple videos a reddit type of clone and that's why I'm going to call it read it or whatever you want to call it there and then I'm going to type in dash D and I want to use MySQL so I'm going to type in MySQL and just hit enter and it's going to generate all the different files you're going to need for you right there now SQL Lite is used database choice by default but like I said everybody normally likes to use MySQL and you can see all the different gems as a ruby gems and if you do not know Ruby for the most part I'm going to cover it in this tutorial but of course I'm going to have in the description I'll link to my Ruby tutorial and definitely check that out it's a video that basically covers the whole entire Ruby programming language in one video so it's pretty easy to use then after you go and create this guy you're going to want to tell something called the bundler to go out and get everything your application is going to need and to do that you're going to type in bundle install like this and if it says it cannot locate the gem file you can come in here clear that out and I'm going to do LS here if you're on Windows type in dir and you're going to change into your read it directory and now come in here and type in bundle install and you're going to see that it's going to go and get a whole bunch of different gem files and load those in there right there let's clear that out while we go and take a look exactly what type of files we have here and to do that we can just type in LS and here are all of the files that were generated for us now the gem file is going to contain all the gems that your application needs gem lock is a file you should never touch because it's going to have all of your gem files in there for the bundler so don't worry about that and the bundler basically just handles getting gem files and other dependencies that your web application needs and for the most part it does it and you don't need to worry about it readme right here you can type in some information about your web application what it does Raik is going to be a command we're going to use a lot and it's going to basically compile our web application app is where most of the code you're going to be creating for your app is going to go it's going to have your views in there for your interfaces the model for the functions and the data and the controller which is going to handle communication between the model and the view so they can act independently the bin directory is going to contain all the files to run your application config is going to configure your routes remember the routes fan between the browser and the proper controller and it's going to send you to the proper controller and you're going to see examples of that here in a second DB is going to contain your database schema which is going to define the tables and the general or of your database configured using to contain configuration rules for starting your application and then all these other different files we'll get into as the tutorial continues now like assets come in here well if we want to open up our whole entire directory I'm going to open everything up in sublime text inside of my terminal node type in su BL and then dot and it's going to load every single file right here all in sublime text which is going to be very easy to use and you can see right here the gem file if we just want to open up the look at it here are all of the different gems and the version numbers you could easily come in here and change the version numbers if you need to but for the most part you're not going to worry about any of that another thing that's pretty useful is if you ever try to execute anything over here inside of your terminal and whatever command you put in there doesn't work very often you can use bundle to help you along just type in bundle exec and then whatever command won't work that's going to help you and probably go in there and get any dependencies that you might need so just something to think about now we would normally go in here and create your web application and launch it inside of the server but because we're using MySQL we're going to have to change a couple little things for our database and where we're going to change those is go over here to config open this up and then open up database dot yml and then inside of this guy we're going to have to make a couple little different changes one thing we're going to need to do is point at the socket we're going to use for MySQL and how we're going to get this is you're going to open up my SQL work bench make sure you get that with MySQL and we're going to launch that and whenever that launches just click up here and you might have to enter your password and then server status and then right here is the socket you're going to use so that's a really easy way to get the socket that you might need we can close this and then we come in here and paste that inside of there another thing we do not have our database set up yet so down here we're going to comment this out for the moment and then right here you're going to want to put in your root password temporarily so whatever that password might be and then you're going to be done with the database dot yml and we'll get more into later on okay so now that that is all set up we can come over in our terminal again and start our server just going to type in rails server like this and hit enter and there you can see that it ran and we're going to be able to shut that down by holding ctrl C what's really great about rails is it's going to use the web brick server and launch everything inside of localhost 3000 so we can just come up here get rid of this hit enter and there you can see rails is installed and ready to be used now we come in here and generate some controllers and views and to do so we're gonna keep our server running you don't have to shut it down or anything come down here inside of the terminal and you're going to type in rails generate controller and then you're going to have to put in a name and other different types of actions that you want also created so I want to create a controller it's called welcome and I want to create an action inside of it which is index and you can temporarily think of this as a directory and then another directory inside of that you're going to see exactly what they look like here in a second and then you're going to hit enter okay so let's take a look at the different files we created here right here you can see our controller we'll look at that up close here real soon you can also see here our view which we'll take a look at here in a second and a couple of tests and helper files as well as assets for Java scripts and stylesheets and so forth were also generated one thing to bring up if you did have a little bit of a problem here is if you would type in rails - version make sure that this comes back as 4.2 again this is only if you have an issue and you also could come in here and type in Ruby - V and make sure that it's 2.2 or something that's later than that if it did not come back as Ruby 2.2 what you're going to want to do is type in our VM use Ruby - 2 point 2.0 or a later version and then execute that to make sure that Ruby's been updated and then to update rails Jim update rails - - and OH - or I with a space - - a no - or doc like that execute that and then make sure that all of your versions of Ruby and rails are all up that's just if you have some sort of issue with the terminal now let's go over and take a look at the controller right so your controller files are going to be in the app remember most things are going to be an app and controllers and there's welcome controller and there it is and you can see index right here which is the action that's assigned to this controller and this is also going to be the method that's going to be executed whenever you call index what this guy is going to do is render review for us for index whenever index is called inside of our application so if we come over here and just type in index you're going to see an error for now but we're going to fix that here and we could also come over here to the views section and welcome there it is and open up our view we'll be playing with both of those as well so now what we need to do is open up the route file so that we will be able to properly route these guys and to do that we'll come down here this is actually in the config directory open up routes and what we're going to do here is come down to the root section and uncomment that that's going to open up our index here by default then we're going to make some temporary changes with route here in a moment so let's just keep everything the way that it is for now and we'll save that and then we'll open up our view again and this is an ARB file or an embedded Ruby file and that just means we'll be able to put HTML as well as Ruby code inside of here so when we just go and take a look well let's come over here for a second get rid of this just to prove that our index has been changed and we'll get the index version of our web application and there it is and that's exactly what we're working with now if you want to execute Ruby code inside of here there's basically three tags you're going to need to have the bracket with % and this is going to be Ruby code that is not going to output that of the screen then you're going to have this % with an equals and this is Ruby code without put and then close that off and then the final one is just going to be a comment so % and a hash symbol and comment there you go now you know all of the different Ruby tags you can use inside air so let's go and do something a little bit let's just leave the comment here just for the heck of it just we see that what we're going to do here is we're going to output some string to the screen let's just go percent equals so that means we're outputting something and inside of here we'll say something like count to ten close that off and we can throw break statements in here or whatever it works with the HTML now let's say we want to do some code execution that isn't going to output anything like we want to have a for loop here that's going to cycle between zero to zero to ten so I'm going to type in eleven times two and as it's sorting through there it's going to put temporary value for this for loop in I and then we'll close that off and now we want to output I to the screen we can just go look at this and equal to I close that off and then we close off our for loop for ourselves there you go if we save that reload this you're going to see count the ten prints out as well as it counts from zero all the way to ten now I want to jump back over into routes dot RB and do a little bit of a change here so that we will be able to do things like so let's say that we come over here and while we are able to come in and type in something like welcome index and have the index file pop up we are not going to be able to come in and say create another view let's just go create another view come over here and let's just copy just this part here and create another view inside of here new file paste that there and we're going to call this sample I'm going to show you how to communicate between views and we'll call this sample as well and then name this sample dot HTML DRB file save as and there it is so we save that there's another view now even though we created sample inside of here if we would go in and then type in sample instead of index you're going to see what happens there's a routing error so we're going to jump over into routes and we'll temporarily comment this out and use a previous version that will allow us to route between multiple different controllers and so forth so we're just going to type in : controller and this works out great whenever we are just starting off with developing Rubi so and then what we're going to do is we're going to say we're going to route to the controller and if there is an action - then go - we want to route to that and then if there is an ID then we're going to round to - there we will allow that to happen and this is going to put a comma and then this is going to be sent via the get method right like that want to save that and now you can see if we load that welcome sample inside of the browser that it does indeed load up so that's good now what we can do is see exactly how we can pass data from the controller to the templates or views whatever you want to call them and how you're going to do that is first let's go and get our controller this guy over here open that up and there it is and let's create a new action down here let's say Dan fine sample and what we're going to do is we're going to be able to pass these using instance variables so let's call this controller message and pass in a value of hello from controller just so we know that our that's coming from and then come down here and end this function now what we're going to be able to do is open up sample HTML drb if we want to get that value that was passed there we could say something like message from controller and then get on the next line and put a hash symbol here at for the instance variable and then controller message right like this and then close that off and this is going to retrieve the value that the controller wanted to pass and you can see right there a message from controller hello from controller one thing to know though is you're not going to be able to send data from the view to the controller but you can do it the other way around now let's take a look at how to link to our index page from our sample basically you're going to use the format of a link underscore two and then you're going to have whatever your link text is and then that's going to be followed by whatever your link is and inside of the link area we're going to actually be able to link referencing the controller in the action using something of the format of controller equal to and then welcome list your controller inside of there and then you could also give in here and type in your action specifically which would be in this situation index we're actually going to do it that way just so that it makes so let's come in here create our link now we have output we want to put on the screen so we're going to put it like this and we're going to go link to and then whatever the link is going to be so let's say link to index like this and then down on the next line we're going to type in controller and point towards welcome and then down on the next line for our action we're going to type in index and then close that off and reload that you can see it go to index is right here if we click on it we went over the index page another thing you can know is since we have the sample inside of the same view container as sample we could actually come in here and get rid of this all together and let's do that save that reload it here we are again make sure we reload it go to index and it's still going to work exactly the same now if we want to change the sample view here to pass additional parameters in a URL so let's say that we wanted to put an ID and we wanted to pass that over we can just type in ID we're using symbols here which is a ruby format and then let's say we just want to pass something random random and that random thing will be my name there we go make sure you have that closed off could save that then we could jump over into the controller part and inside of the index area we could say something like if params and has key for ID and params has random I thought it just change this to random like this well in that situation we're going to create an instance variable again and go params ID and random instance variable as well Bram's random we could use either the ID or the symbol itself then we want to make those available and we'll jump over inside of index we can do the same sort of thing so we might as well just copy this right there jump back over here so if we're gonna have to put this side of our braces here our tag if params has key ID and random close that off throw a break statement in there then we could go and get the value that was passed here using the instance variable just gotta go in ID : and then we could do the same thing params will you pass numerous parameters of course ID only need one curly brace here copy this do this the same way then we're going to do the same thing for random as well this there and then we'll type in random change this to random and of course we have to close off this if statement then write like that now we can jump over here reload there is sample click on go index oops got a little bit of an error there let's come back over here that's right forgot that one little bracer reload it again go index and there you can see 5 was passed over with the ID and random was passed over with there that's just one way to communicate between all of these different views as well as the controller now what I'm going to do is set up the MySQL database that we're going to be using here so just jump over in the terminal as well open this up fullscreen and if I want to log in to MySQL I need to type in MySQL 5 you might need to type in just MySQL it's up to you I'm going to log in here first off as the root user and then P and R in password for the root and there is MySQL all opened up and ready for us to create a user just specifically for our web application we're working with here the first thing we want to do is come in and create our database and I thought it would be good to cover all this stuff and I'm going to call this reddit development because this name right here is or read it whatever is a normal convention for naming things whenever you're working with rails and that's how easy that is create that we could then say something like show databases and you're going to see reddit development is right there there scroll back if I want to switch over to my new database that I just created write it underscore development there it is and now I want to create a user specific lift just for this database so I'm going to say grant all privileges on and we're gonna call this reddit development dot star always do the same there too and let's create a new user called reddit admin at symbol local host identified by and then you're going to type in a password for this user so let's just make it password just for now and put the semicolon in there now what we can do is say exit leave MySQL and then log back in as that user read it admin in this situation and there we are back inside of here and now we can come in and say use reddit development and now that we have all that setup we can go over into the database yml file and correct all of our database issues alright so here we are in database dot yml first thing I'm going to do is come in here and type in our new username for the guy that we just created and we call it reddit admin going to type in the password for this of course use a better password than that reddit development C I use the same database name for creating the database is what we previously had there generated automatically for rails save that ok and inside of the terminal we are going to call a rake DB : schema : dump and what this is going to do is connect our web application to our database as well as create a schema dot RB file in our DB directory so that our database can be portable to any other hosting company or wherever we want to host this guy and there it is creating that right now and everything is all set up so now what we go in here is a final thing and create a database table and demonstrate what migrations are exactly how they work I'm not going to use this anymore so just close that up now basically inside of rails whenever you create a table you're going to automatically generate instructions for both creating as well as deleting database tables and this is like I said before going to allow us to move our application to another environment and recreate all the tables that we need now to generate our user model which I'm going to use for our read it reddit type of knockoff I'm going to need to come over here and generate a model once again the server is still running over here inside of this other tab didn't have to shut that down at all to be able to do all these other different things now if we want to come in here and generate a model which is going to represent our users in our little web application we're gonna type in rails generate model and I'm going to call this just basically a user let's make it uppercase user and hit enter and you can see right here all the different files that are created there is our migrate file and if we come over here to DB for our database and migrate you're going to see the migrate file right there we're going to do inside of here now is define all of the different things that are going to go inside of our database this guy right here is automatically going to generate timestamps for whenever a user is first created as well as anytime a user is updated which we'll see when we look at the database and just to keep this very simple I'm going to come in here and create a string which is what I want my emails to be represented as and I'm going to store an email inside of here for our database and you can put in all sorts of other different options like let's say that I want the maximum size to be 50 type in limit there with 50 and let's say that I also don't want this to have a value of false and if you've ever worked with a database you're gonna know exactly what this is doing if you want to create another column in another different way could also go in here and type in column general and I'm also going to want to store a password for my users and then come in and put string here see here I put in string there here I'm putting a string there just defining the type of data that I want to store inside of here and I could also come in and of course do a limit and let's say that we also don't want that to be null and let's say that we want this to be a password maximum length of 30 instead of 50 this guy right here the timestamps thing this is actually just like by putting that in there is just like if you say if you would go in and go t dot date time created created at and then also went and created another one of these guys that was called updated at but you don't need those just one a foot adjust like that if you want to with the different data types you can use here instead of strings you could use strings of course and you could use binary boolean data type date/time of course already covered that decimals float integer and why we want to do this inside of rails rather than creating these different tables inside of the database themselves is rales is going to allow us to switch databases if we need it's going to automatically generate this for multiple different databases and then we have text and then we have just basic time so those are all the different data types that are available whenever we're trying to create this and they're automatically going to work with the proper database no matter what you would use and in regards to options you have see I had limit here and here I have null you're also going to be able to come in and go default and create a default value so whatever that value would be and for decimals you would also be able to come in and go precision and whatever that value would be and you'd also be able to put scale inside of there and once again that's just for decimals and what's also cool with rails is your auto incrementing IDs those are automatically going to be created for all of the different records every single time they're updated for your users table you have here another thing we could do is like I said we're going to be able to recreate our tables but we also could come in here and define exactly what would happen to end or delete our tables so let's come in here move that end up there to the proper place and instead of change here what we can do is define this as up and then come in here and define another method as down and what's going to happen here is this is going to allow us to come in and drop our tables and that's how easy it is to drop our tables there's end so you just leave it the way it is or you can use up-and-down and be able to traverse backwards and forwards now if we want to come in here and execute our migration file clear that off and create our tables for ourselves you just go rake DB and migrate and you can see right there the table was created actually you could go into MySQL open this up we're in MySQL no use read it development and we could say describe users and you can see it automatically created the auto incrementing ID the email the password there's created two ATS and there's updated at so it automatically created all of those things for us inside of our database and if we jump over here you're going to see our migration file right here so inside of here open that up and you can see right there where it creates our users table inside of our database another thing that's interesting is we would actually be able to revert backwards and forwards between these different migrations and I might as well cover that here as I finish off clear that of course get out of my SQL and if you wanted to jump over and revert back to a previous migration file let's say when to revert the whole way back to nothing so that the users table isn't even generated you just type in DB migrate version equal to zero that would take you back to the very end and otherwise if you wanted to issue a migration again what we would do is just get our timestamp for our previous migration how we do that is DB migrate colon status this is going to show us all of our different migration files which is just the different states of our database creation that's all say there was create users so if you wanted to come in and revert back to another migration just go migrate version is equal to and then take this guy right here and revert back to that but we're already there so we don't need to do that alright guys so there is sort of like a run-through of a whole bunch of different things you just an overview of using rails and a whole bunch of different tools you would need in the next part of the tutorial I'm going to continue making our little read it type of web application and cover pretty much everything else please leave your questions and comments below otherwise until next time
Info
Channel: Derek Banas
Views: 576,198
Rating: undefined out of 5
Keywords: Ruby on Rails Tutorial, Rails Tutorial, Ruby On Rails (Software), Ruby on Rails Video Tutorial, Rails Video Tutorial, Rails 4, Rails 4 Tutorial, Ruby (Programming Language)
Id: GY7Ps8fqGdc
Channel Id: undefined
Length: 29min 24sec (1764 seconds)
Published: Wed Mar 04 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.