sehr kleines bad einrichten

sehr kleines bad einrichten

>> [music playing] speaker: awesome. so today, i'm going to betalking about ruby on rails. so, ruby on rails is a web framework. it's basically a way to make awesomeweb apps, which a lot of you guys will probably be doingfor your final projects. >> so it's also cool timing,because you're currently working in php for this current problem set. so, hopefully, a lot of thesethings that i'm talking about today


will ring some sortsof bells, and there'll be some carryover between the stuffyou'll learn today, the stuff you're working on the problem sets. >> all right. so, what's on the agenda? first, we're going to go over areview of mvc, model-view-controller. hopefully, you've learnedabout this some in lecture, but it'll be a review of that. >> then we're going to talka little bit about rails,


which there are an infinite numberof things i could talk about. so we're going to go over some of thebasic, what is rails, what does it do. >> and a little bit about ruby2, because ruby on rails is, not surprisingly, built ina language called "ruby." luckily, ruby is a lot moreintuitive than a lot of the languages you've been working in. so hopefully, it shouldbe pretty easy to pick up. >> and last, we're going to spendtime going over a basic web app, and digging into some code there.


>> so, this is the official guide to mvc. this is what you'll findon study 50 and lectures. and mvc, obviously, has three parts. so there's the model, persistentstorage, managing and organizing data. you can think about this as your mysqldatabase in the current problem set, any data files you have. there's the view, which is thepresentation of information, the user interface, and you can thinkabout these as html pages and any of the template pages you'rebuilding on this problem set.


and the controller, whichhandles all of the information, kind of passes information betweenthe databases and the view. >> and you can think about theseas your current php files in the programs you'rewriting for problem set 7. so i'm going to go into an unofficialguide, which is inspired, actually, by last year's ruby on rails seminar. i really liked the way they kind ofthought about model-view-controller. so, let's go now, intothe unofficial guide. >> so, we have a couple charactersin the unofficial guide.


there is our "model," madeline, whois very, very smart, loves data, knows all about data, butshe's a little bit anti-social, and she doesn't usuallyinteract much with people, unless she's told to do so. >> next, there's our "view,"vicki, who is gorgeous. she's absolutely gorgeous,but she's not so smart. and lastly there's our "controller." hopefully, all of your controllers willbe less nefarious than this controller here, but this is ourevil queen, caitlyn,


who really likes ordering peoplearound, but doesn't really like doing things herself. >> so, let's start with an analogy. let's say all of ourcharacters work at cvs. there are two scenarioswe can think about. for some reason, i have scenario 2,scenario 1, but that's all right, we'll start with scenario 2. >> so, let's say a customer comes up tocvs, and vicki is working in the front. she's the cashier.


so, the customer asksvicki for a piece of candy. you know how, in cvs there's alwaysthat candy lined up in the front row. so a customer asks for that candy. what happens? >> well, vicki doesn't know what todo, so she turns around and asks caitlyn. "caitlyn, what do i do? this customer just asked for candy." caitlyn, our very controlling managersays, "well, the candy is right there, vicki, just give it to them.


and so vicki says, "oh, okay, good," shehands the candy back to the customer. that's kind of our first scenario. so, in this case, we're noteven dealing with her model this is just, say, a user went andclicked on a link to your web page. the view is stupid. it doesn't really know what to do,so it has to go to the controller. it has to ask, "what do ido with this information?" the controller then handsback the appropriate page, and the view can display it.


>> we now have a secondscenario, or our scenario 1, in this case, where a customerasks vicki, the cashier, for a cup that's sitting inthe back, somewhere in storage. now, again, vicki says, "oh my gosh. i have no idea what to do." >> she turns to caitlyn, themanager, she says, "caitlyn, i have absolutely no idea what to do. they asked for a cupthis time, what do i do?" caitlyn says, "all right, hang on."


she turns around, yellsat madeline, our model, she says, "madeline, goget a cut from the back." madeleine says, "okay, okay." >> madeline goes, she knowsexactly where the cups are, she grabs the cup she hands itback to caitlyn, our manager. the manager hands it back to vicki. vicki now can finally present thecup to the user, or the customer. so, in this case, it'sslightly more complicated. we had a couple more parts, butbasically the idea is pretty similar.


something happens on the front end. the user, the customerasks for something. the view doesn't know what to do,it has to go to the controller. the controller tellsthe view whether just to hand back a certainamount of data to the user, or to go get some data from theback, hand it back to the view, and the view can present to the user. >> so i think that's a usefulway of thinking about mvc. >> now that we have that framework inmind, let's talk a little bit about ruby


on rails. ruby itself, ruby the language, hasa lot less syntax than php or c, and the things you've been working on. you'll see there are a lotfewer semicolons, a lot fewer curly braces, which makesit a lot nicer to work with. it reads a lot more like english. >> also, ruby has somepretty fun features called gems, which you can think aboutkind of like libraries in c, or the things you include withthe hash up at the top your files.


so, there are ruby gemsfor just about everything. it's a super well-documented language,and has lots of people working on it. >> so, if you are thinking aboutimplementing some cool feature, try a little googling,and there will probably be a library for it that has somepre-built-in functions that make whatever you want to do a lot easier. and we'll talk a littlebit about some that you can use to enhancethe web app that we're going to talk about a little bit later.


ruby on rails, i said, is a framework. what is a framework? a framework is, basically,just a default structure. it gives you a set of filesand a set of procedures for managing data, managingyour views, and just the general structure of your program. so database, webservice, web page is kind of sets up a big chunkof your project for you, and then you can go in and kind ofchange the things that you want.


>> so, that leads to kind of thesecond big pillar of rails, which is convention over configuration. so, ruby on rails, the developers inthe community around ruby on rails, values convention over configuration. and what that means is, thereare standard procedures and ways of doing things inrails that are generally accepted as the way youshould, or the easiest way to going about doing things. >> and you can see that, when we'llactually start generating our app,


like i said, there are a bunch offiles that are already built for you. and this is kind ofthe convention thing. it makes it a lot easierto navigate around your app and find ways of doingthings, if there's a set structure, a set ofprocedures that you normally follow. that said there's a lot ofconfiguration you can do in rails. there's a lot of personalisation, alot of big-- twitter's built in rails. there are a bunch of otherbig companies that use rails, so this isn't to say that railscan't do whatever you want it to do,


it's just that, generally,there's a set procedure that makes it a lot easier if you follow it. there's really good rails documentation. i'll point you to a couple other usefulsources later in the presentation, but this is just a link to theofficial ruby on rails documentation. definitely check it out if you'rethinking of working in rails. a little bit about ruby. we'll address differencesbetween ruby and the languages you've been working in morewhen we get into code examples,


but this is just kind of acool thing that ruby does, which is iterating throughnumbers and an array. >> so if you remember, in c,what you have to do is, you have to set up a for loop, andyou have to iterate through your array by accessing each element. so you have array [0], orarray [1], or array [2]. in ruby, they've taken careof a lot of that for you. so for a for loop, youcan instead use .each. >> so, if i have an array numbers,and it has five numbers in it,


and i want to go through every numberand print it out, i can just say, numbers.each do number, whichsounds a little cryptic at first, but basically, what that'sdoing is, it's saying, i have an array of numbers,i want to take each one, and instead of saying numbers [0],numbers [1], i can just say number. and it's going to gothrough each one and call it "number" as it goes through it. so then, i can just say, puts number,and the first time through, it'll puts the number, which is one.


the second time through, it'llputs the number, which is two. third time through, et cetera. >> so this is just a fairlycommon construction in ruby, so i thought i'd justshow it to you guys, so it's not overwhelming if you find it. and the last difference is, atthe end instead of curly braces, we say, all right, we're donewith our for loop, let's say end. and we're telling ourprogram that, okay, we're done with this chunk of code.


>> so just a little bit of exposure todifferent types of syntax in ruby. >> okay. we are going to now just divestraight into an example, and spend the next 40 minutes just kindof digging into it, going through it, explaining things. if you guys are here for the seminar,do you want to move up into the couches? cool, awesome. and feel free to stop me at anypoint if you have questions. raise a hand, whatever, just shout out.


>> ok or actually, let's go back here. >> so, i've put a link toall of the code that i've written for this example on awebsite called github, which if you haven't beento github before, it's basically just a place whereyou can store a lot of code, and interact with it. it has a lot of cool features, but fornow that's all that's really necessary. >> so, there are two thingsthat you can-- basically, two options for dealingwith this code-- you


can visit github.com, andfor their instructions that you'll see for how to followalong and build your own app, that's the same as theapp in the example code, or you can actuallydownload all of the code and there are examples for how to justrun it right off the bat if you want. or sorry, instructions for howto just run it right off the bat, if you want experiment and just kindof see what it does on your own. we'll also be playingaround with it some today. >> so, in order to just get a copy of allof the code that's in this repository,


all you have to do is type thisline into your command line, and these slides will be availableafter, so no worries there. so you can just copy thisline into your command line. it'll make a new folder withall of the example code, or you can download it from the websitebecause it'll be, from the cs50 website because it'll be upthere afterwards too. >> all right, let's look at some code. okay. so this is what thegithub link looks like.


if you went to the first link thatwas just on the slide just displayed. so, it tells you aboutthe app, what's in it, and then what's the four different kindof folders that you'll find in the app. >> so, let's take a look atthe first folder in app. so to-do list, starting out. basically, what this app is goingto do is, it's pretty simple, it's just going to make a to-do list. >> and on your to-do list,you'll be able to add items, you'll be able to delete items, you'llbe able to view individual items,


edit items, and see allof the items in your list. >> so, fairly simple functionality,but this is what rails is best at. well, rails is good at alot of things, but this is kind of the foundationof rails, and once you know how to kind ofdo these four actions: create, read, edit anddestroy, it's kind of-- i always find "destroy"a really violent word. i don't know why they just use"delete," but anyway, the convention is to say "destroy" so we'regoing to destroy items.


but once you can do these fourthings, basically, the world is open to you in web development,and so it should be a useful example. all right. >> so, actually, first, i'm going toshow you what this, in practice, what this to-do list is going to do. and, i'll explain. so, if we go to, i alwaysforget the exact syntax for starting a rails server,but that's why i wrote it down. so, this is just starting a server.


you guys are all doing somethingsimilar on the problem sets, not this exact syntax. we'll go into it a little more later. >> okay, so this is what thefinal app is going to do. we are going to be welcomed to oursite, we can view our to-do list. right now, it's empty,so we can add an item. let's say, "go to cs50 seminar today." save item. >> all right, so now we're seeingexactly what we just wrote.


it makes sense. and then, we can edit it if we want,or we could just go back to do list, and now we see the itemis, "go to cs50 today." or sorry, to make ita little clearer, we could add a little break here, "goto cs50 seminar," the text is today. we can also delete this item,and now our to-do list is empty. >> so again, pretty basic. there are some, i'm not sure if thisyear there's a seminar on bootstrap, but if you want to makeyour site beautiful,


you can use things likehtml, css, and bootstrap. definitely view those seminars if you'reinterested in making prettier apps, but we're working onfunctionality today. >> all right, so let's get started. so, if you go to, again fromthis home page on github, you could say "liststarting out," and this will walk you through just the basicsetting up your framework in rails, starting your server, and gettingsomething to display on the screen. >> so this is just the basic setup.


so, making a new railsproject is really easy. all you have to do, sinceit's pre-installed on the ide, is type "rails new" andthen your project name. and that's going to generatea whole bunch of folders. >> as you can see, if i goto todo_list_starting_out, we have, it can be kind ofoverwhelming, because there's just a ton of stuff that just appears. luckily, you don't have todeal with too much of it. especially, when you're startingout just building basic web apps.


>> the main folder you'll beconcerned with is this app folder. and you can see in this appfolder, we have a couple things that already look familiar. so, we have models,views, and controllers. >> so you can guess where the differentcode for models, views, and controllers are going to go, and it's probablygoing to be in the appropriate folders. so, the first thing wewant to do, is basically, we just want to make somethingappear on the screen. in this section, we just want towelcome someone comes to our home page,


just say hello. >> so, the way you do that is, you cangenerate a controller really easily in rails. right now, we don'thave any controllers. or, if you've just gone this far,you won't have any controllers. if you go to yourcontrollers folder, all it will have is this applicationcontroller, which is just telling you, you can do some base configurationin your application controller, but it's not going to tellyour app what to do if someone


clicks on a specific web page. >> so, the way that youset that it up, is there is a line you can run in rails,which basically, just says, generate a controller. make me a file that's going to helpme perform the functionality that i need on this website. and, in this particular one,we're making a welcome controller, because all it's going to dois welcome users to the page. and so, you generally want to name yourcontroller something that makes sense


for the functionalityit's going to perform, and within that welcome controller,we want to have an index action. now, if you remember fromyour server problem set, one of the things you had to do, was checkto see if a user went to a home page without, so if they justwent to a directory, you had to check forindex.html or index.php. that's because, byconvention, "home pages" are called "index" in web development. >> so, you can think of index.anything,really as a home page.


so we're going to say, we wantto be able to handle requests to this home page. and we want to have an action withinour controller that can deal with that. so what does that look like. >> well, once you run thisline of code, you're going to get a welcome controller. and, within that welcome controller,it defines a method index. and that's all it does. it just says it exists.


and that's because, we don't reallyneed any fancy functionality when the people want to go to our home page,we just want to show them the page. >> so, we just need to tell our controller,"all right, this action exists. this page exists. don't freak out when youget here and just show us this index page," which isall well and good, except we haven't made an index page yet. >> so that goes to the nextthing we have to do, which is once you set up your controllerin step 3, you have to go to the views,


because, although you'vetold your app what to do when they go to the indexpage, you haven't made an index page. >> all of our views, likei said before, are going to be stored in this views folder. and, as you can see, wealready have a welcome folder. that was generated when wegenerated our controller. >> it assumed you're probablygoing to want to show people views with the various thingsyou want to do in this controller. so, we're going to make a folder foryou that's related to this controller


that you made. and it's going to be called"welcome" because your controller was called welcome. >> you can put in any pages you want here. so, like i said, we want an index page. we want a home page. so, we can create this index page. it won't be here originallywhen you first type this, so you'll have to make it.


and you want it to becalled index.html.erb because that's kind the rails way ofsaying, this is a rails view page. >> so instead of just index.html,the convention is index.html.erb. and, you can put whateveryou want on this home page. right now, all i've said is "hello." >> so that means, what happens,let's see this in action. let's stop our server from ourfinal to-do list, go to our starting out to-do list, andstart the server again. maybe.


let's see. here we go. so now our server should be running, andit's going to be serving us this basic, just starting out page. so, when we visit the homepage remember, we've-- >> oh, sorry. there's one otherimportant area that you need to configure when you'restarting out your rails app. so, we've told thecontroller what to expect


when we get passed this information. but we haven't actually configuredthe routes of our web app yet. >> so, basically, we need to makesure that the controller gets passed the right informationat the right time. so, how does our web app even knowthat when we go to the home page, it should tell the welcome controllerto be okay with the index page, and then trigger the index page? >> right now, if we went toour home page, our app doesn't really know thatit's supposed to talk


to the welcome controller at all. so, the last thing,kind of important area to edit when you're settingup an app is routes. and that's in config, routes.rb. >> so, i do have thatcorrect in my directions on this getting started page, so ifyou're following along with that, that should be easily discovered. >> so, the one thing we haveto do, is we have to say, when you first arrive at this page,this line will be commented out,


because there are a lot of comments. again, this is just part ofwhen rails gets generated, it puts a whole bunch of stuff in here. it has all this stuff commentedout, because you probably don't need it originally, but thisis kind of stuff that a lot of people tend to use. >> so, you can delete allof this if you want, but you'll find someof the things you might want to uncomment at various points,just because they tend to be useful.


so, this is something thatrails just sets up for you. >> so, this will be a line that'scommented out, but you can uncomment. and, what this line says is, allright, if the user goes to the root, goes to our home page, we want totrigger the welcome controllers index method. and that's how this all ties together. >> user goes to the home page. in routes, you set it up so that willtrigger the welcome controller, that will go to welcome controller,which will then say, all right.


index, i want to call index.html.erband give that back to the user. >> seems kind of convoluted, but you'llstart to see a kind of pattern of, we have to do something with routes, wehave to do something with controller, we have to do something with the views. and then later, we'll add in,okay, once we have all our routes, views, controller, model, controller,views, controller, model, controller, views. >> so, that's kind of apattern you'll start to see. audience 1: can i ask a question?


speaker: yeah, go for it. audience 1: two. one, is that i noticed yourestarted the server, after you put in the index thing. does that mean every time you changesome code, you need to restart it? >> speaker: so, it, often times,especially if you're just kind of changing around views,you just need to refresh the page. sometimes, if you makesome major change, you'll need to restart the server.


generally, if something isn'tshowing up that you think it should, just restart theserver and often it'll come up. >> audience 1: so, just restart it. >> speaker: yeah, usually, yeah. basically, usually, i just restart theserver if i run into some sort issue. >> audience 1: and, my secondquestion is, it seems, as you said, seems a pretty convolutedprocess to fix your index page. now, i assume there's a lot of thingsthat makes it powerful and versatile, but i haven't seen that part yet.


will you explain a little bit? >> audience 2: go ahead andreiterate his question. >> speaker: oh, sorry. so, the first question was,i restarted the server, and why did i do thatat that particular time? the specific reason for thatone is because i was actually changing folders, and so i had torestart it for the correct app. but usually, you just have to restartthe server if you make big changes. >> the second question is, thisdoes seem really convoluted.


why is it so complicated to makejust a single web page appear and where is the power of rails? >> so, basically, the powerrails is going to come in one of the next things i'mgoing to show you, basically. so, although this does seem convoluted,and especially compared to your problem sets, it's actuallya lot more convoluted in almost any other framework. cs50 has kind of taken careof a lot of this back end for you, when they've generatedthe code for your cs50 finance.


so, a lot of this has just gone underthe hood in what you're working in, because they wanted you guys tofocus on model-view-controller, just those aspects, because thatis the base of web development. and it's really important to understandthat stuff and how it fits together. >> however, there's a lot ofstuff under the hood that has to go in before you can evenget to model-view-controller. so, that's kind of whatyou're seeing here. and really, if you lookstep-by-step at the directions, it actually only is about four steps.


you have to generate the framework,so just type "rails new." you have to generate a controller,you have to generate an index, and then you have to create a route. >> so it's really only editing four files. and a lot of the set up is done for you. so it also seems more convoluted,because it's taken a lot more time, since i've tried to explain thevarious steps and why they happen. so, if you think about itonly as of those four steps, it seems less convoluted.


and also, what i'm aboutto show you, is kind part of the reason why peoplelove rails so much. >> okay, so let's just first see this pagethat we've worked so hard to create. "hello!" all right, very underwhelming. okay, let's continue. so, after the second part,after to-do list starting out, is to-do list with items. >> so, if it's goingto-do list, it probably


needs some items in the to-do list. so, the first thing we have to do,or one of the first things to do is to make a series of routes,because you're probably going to want a page whereyou can make new items. >> you're probably going to want to pagewhere you can see the individual items. you're probably going to want apage where you can edit the items, and then obviously, deleting is,you might not want a page for that, but you're going to want to be ableto click on something to delete items, and have that information available.


>> so, this is a really usefulthing that rails does. so, if we go into the next folder, whichis with items, we can see in our routes again. config, routes. we've added something new. so before, we just uncommentedthis root welcome index. and that was again saying,if you go to the home page, go to the welcome controller,go to the index page. now we have this three extra lines.


they look a little cryptic. it's kind of unclear what they're doing. but basically, this iscreating a rails resource. >> and a resource is just a setof instructions and procedures relating to a specific,sorry, let me rephrase this. a resource is a set of proceduresrelating to some construct that you'll want to have. so in our case, it's items. we want to have items; we wantto do a bunch of things to them.


>> and so what this couplelines does, is it says, we want to be ableto have a bunch of paths that we can go to do differentthings with our items. and it creates all these paths for us. so, one way of demonstratingthat, is if we go -- let's stop our server so i canuse the terminal-- rake routes. what this command does, isit, i'm still in starting out. >> but this is, we canstart with this actually. so, as you can see, this showed us thatwe have, we can go to our home page,


and that will trigger thewelcome controllers index method, but we can also go to welcome/index,and that will do the same thing. >> so this is two differentroutes that you can type in, in order to get to the same home page. so this is showing us all are availableroutes in our starting out app. now, if we go to our new app that hasitems in it, so todo_list_with_items. >> and then we run rake routes again. so, to show us all the availableroute to the user can type in. now, we have a bunch more.


and all we've done is addedthose couple lines in our routes, but now we can go toslash items from our root, and we'll see index pagethe items controller. or, we'll trigger the indexaction in the items controller. we have "new," we have"edit," we have "delete." >> so, this has created abunch of available routes. now, we haven't made ourindex controller yet, so we haven't actually made any valid,we haven't made these actions valid. but now we don't have to add adifferent line in our routes controller


for every single one of theseactions, not in our routes controller, in our routes file. >> so the system generatesthis automatically for you. all you have to do isedit your routes to rb. already, we're seeing oneof the perks of rails. it does all this for us. and now, all we have todo, like we did before, is edit our controller so we canhandle all of these different paths that the user may want to take, andedit our views so that we have files


that the controller can call. let's go to, we wantto edit our controller. right. that's exactly what i just said. or make a controller. >> so, you'll see right now, i have a newcontroller called "items controller." and i generated thatvery similarly to the way that i generated theprevious controller. so, if you go through theinstructions in part two, with items,


you'll see that after you edit theroutes, you make a new controller. and, if you remember the syntaxfrom the previous example, great. if not, fine. >> bin/rails generate controller items. this is a little different, becausewe didn't say index right after, because we just want togenerate a blank controller, because we'll be doing a lot ofconfiguration within this controller. so we didn't want to preset indexmethod in it just on the start, but that's a pretty small difference.


basically, it's exactly the same. we're making a controller so that we canhandle any requests to deal with items, and that will also make a file inour views to deal with controllers, or to deal with items. >> can i get a gauge? how are people doing? it's a lot of information. is a generally making sense? are there things that are justabsolutely way too confusing


that i should go over? the purpose of something? doing okay? we'll go on then. we've made an items controller. we want to deal with items. what do we want to do with our items? well, we want to beable to make new items, and want to be able to create items.


these seem kind of similar. what's the difference? >> well, there are a couple differentways that you can channel information into a controller. one, is through getrequests, so through urls. and we already saw that. we went to welcome/index, we gotour welcome controllers index page. that was information channeled tothe controller through a get request. >> there also post requests, though.


and that's if you submita form online, there's still information that sent backand forth to the controller, but it doesn't necessarilygo through the url. so there are a couple differentthings we can do with these items. we need a page to goto where we can type in the information we want forthe new item in our to-do list, but we also need to beable to handle, once we click enter, what happens with thisinformation that we've typed in. so that's kind of the generalpurpose of "new" and "create"


"new" tends to be apage that you'll visit. it's information that gets passedto the controller through a url. so when you go to /new, your routes areset up to handle that as information that will get passed tothe items controller, and then you'll kind of render a pagethat shows a form that the user can type in information. >> but then there's "create." so once you type in yourinformation and press submit, create tends to be theaction that will actually


take that information that you'veentered, and do something with it. so that's the distinction between thosetwo which can be originally confusing. >> so, all of that our createmethod does right now, is render, plane, params, item, inspect. all that means, is take the informationyou got relating to item, this item that we typed ininformation for, and we'll show you how that works in amoment, and just show it to me. so after you type ininformation on this new page show me what you typedin on the next page.


>> and, we can see that in action. let me close some of theseso it gets less cluttered. let's go. oh, we're in with items. so let's start our server. i've just gone to items/new. this is telling, because of theway we set up our routes in config, this path is set up to giveinformation to the items controller. and specifically, the new actionwithin the items controller.


so, if you remember all thatsaid, it was "def new end." so that's just going totry and find new.html.erb, the html page that relates to theaction that we're looking for. and i'll show you that page in a moment. but as you can see it justsays make a form, basically. >> that's what that page does. we can type in another thing. so "new item" and "made today again." we're going to save it, and we seeall that our create action does


is print it out to the screen right now. eventually, in the next section,we're going to put into a database, but for now, we can see we were ableto take that information and handle it. >> and so again, this is stillkind of underwhelming, but at least we now can kindof start dealing with data. so, what does our new page look like? what does the htmllook like on that page? >> so again, you kind ofexpect that to be in views, because it's dealingwith information that


actually gets displayed on the screen. and when we generatedour items controller, that made a handy folder for usin views that relates to items, and i made this new.html.erb page. >> so, this is another reasonwhy rails is awesome. it's really easy to submit andtransfer data dealing with resources that you made in routes. so, all we have to do, inorder to tell the app that this is information about our to-dolist items, is say form_for: item.


that's it. >> and then, we're just tellingit what url to direct to after we're done submittingthe form, and it's items_path. and we can look at whatthat is in a second. but for now, let's justgo out to the form. >> so, if you remember from when iwas talking about ruby for loops, it was array.each do number, and then,every time we went through the loop, it did something to, or it printedout the next number in the list. >> this is similar syntax doingsomething slightly different.


it's saying, basically, all it's sayingis, i want to relate this form to, it's giving, right. let me rephrase again. >> it's relating the form items, sothese things we see to this form, that we've made for item. so it's just a way of kindof tying everything together. i'm not going to gointo it too much detail. but basically, what we'vedone is we're saying, for this form that we've made for thisitem, we want to have a title field,


and the label will be "title,"and it'll be a text field. >> and this, all that this syntax isdoing, is kind of generating html. you'll see it doesn't looklike html, mostly because of these kind of weird symbolson either end of the text. and what that's saying is, weare going to want ruby code, or we're going to want htmleventually, but we want to use some ruby to generate that html. >> and so this is just stickingruby code into html. so, we can see this is plainhtml, h1 new item close h1.


that looks familiar. that's stuff we've seen in lecture. we can see these p-tags thatalso look pretty familiar, but the form itself, the htmlis mainly generated by ruby code that you've stuck in there. >> and you can see, actuallywhy don't we do this. i think we can do this. this may be small. yeah, this is probably too small,but what you can do on your own,


is you can inspect the htmlof a page by right-clicking, and then clicking inspect element. and you can see the actualhtml that our ruby code built. and you'll see there is a formtag, there is a label tag, there's a bunch of, atext field tag, and so you'll see of the resultsof this html that we created using ruby, which is kind of cool. >> all right, and, i believe that'sit, for just making a new item and showing on the screen.


any questions now, beforewe get into our last chunk, which is dealing with databases? no? okay, cool. >> and as you saw again, it actuallyreally only took a couple steps. it took generating a controller,adding a "new" and "create" action, so that the controller coulddeal with requests for items/new and so that the controllercould deal with information that came in after submit ofthis data relating to our item.


and then, making the form. >> so, pretty simple actually, even thoughagain, it seems like a lot of steps. >> audience 1: did you alreadycreate a model for items? >> speaker: no, so, wedon't have a model yet. right now, all we have is thisrails construct, a resource, that really can take any shape. so, what we're aboutto do is make a model, so that we can putthis resource, an item, into a database with a set structure.


>> so the way that creatingmodels work in rails, let me go to the next, so to-dolist with mvc see part 3 of 3. >> so the way you create a modelin rails is, again pretty easy, it's kind of a one-line thing. you say "rails generate model,"similar to "rails generate controller." you give it a name. we're going to call it "item." and again, by convention, youusually capitalize and don't pluralize the names of models.


you can call them whenever youwant, but just by convention, and so it makes your codemore readable by others, generally this is the convention. >> and then, you tell it,basically, what columns do you want in your databasefor this particular model. so we're saying, we wanta title column, and that's going to hold strings, which, notsurprisingly, is similar to the string data type that you worked withthat cs50 library implemented. it's just a set of characters.


>> and we're going to have a notes column. so, we're kind of items andthen notes about those items. and that's just going to be a bunchof text, so similar to string, but just gives you more space. >> so, you can think about this assetting up the structure of a database. this hasn't actually made a table yet. all this does is says, i'mgoing to make a file that's going to basically be ableto be made into a database once you kind of confirmthat everything's okay.


>> so, if you run this, it makesa file with a really long name. it's going to be in the databasefolder, in the migrate folder, and it's going to havea bunch of numbers, and then say create-- this isa typo, it should be items-- but, it's going to looksomething like this. this is the format that it'll look like. so it's a bunch of numbers,and then create, and then whatever your model name is. >> and if we go to that file, it won'tbe in this one, it will be in mvc one.


if we go to that file,migrate, we can see that this file is now set up to createa table with a string column called "title" and a textcolumn called "notes." so we're all set upto make our database. we haven't yet, butwe're set up to do it. and then, in order to make it, luckily,this is all set up for you on the ides, but basically, rails by defaultuses a database called sqlite. often, when you push toproduction mode you'll want to switch to different databases,but for the sake of this final project,


and honestly, for small webapps in general, sqlite is fine. >> and it has the word sql init, so, not surprisingly, it's kind of similar to the mysqldatabases you're using. it's kind of a lighter version of it. >> another beauty of ruby isthat you don't actually have to deal with any sql whatsoever. ruby deals with all of those. i think you've seen at thispoint "insert table at" and you have a bunchof long sql commands.


ruby does that for you which is amazing. or sorry, not ruby. rails does that foryou, which is awesome. so you don't have to deal with anysql when you're dealing with ruby. >> so we have this file, whichwe don't need to edit, because we already set it up whenwe said "rails generate controller" and we gave it the types. and now, we can create this database. and the way you do that, is byrunning the command "rake db:migrate."


and that says, actually makethis table, with these columns, this set up, that i cannow insert data into. at this point, you'vemade your table, and it's ready to be queried in any way you want. so, not surprisingly,we have a database, but we don't reallyknow what to do with it. where do you think we go? which of our mvc tells us whatwe're going to do with information? >> audience 1: controllers?


speaker: controllers, yeah. so we're going to goback to our controller in order to set up a way toactually put data into the database, and take data out of the database. and we don't have to makea new controller this time, because this is all stilldealing with our items, right? we're putting items in,we're taking items out, so it's still in our items controller. >> and we can see, let's go to app,controllers, items controller.


there are a lot of changes,but the important ones are, we have a "show," which now accessesthis item model that we made. so remember, we called ourmodel item with a capital i. and this is basically takingcare of the sql command for you. this is saying go to the database,get out an item with the specific id that i'm looking for. >> and it knows which idyou're looking for by way of passing data in through the"view" which i'll show you later. and, also now, our "create"looks a little different.


we are now, again, dealingwith a sql command. we're saying, "put this newitem into the database." >> and, this is basicallyjust for security reasons. we have a private methodthat just kind of takes care of making sure inserting andtaking things out of the database is done safely. so, you don't really haveto know too much about that, but it's just a security thing. >> and, we're going to savean item to the database,


and then redirect to the show page. >> so this is a lot to take in. i think i'm going to stop here,and pause, talk a little bit more about, generally, the structure thatwe've seen, and a couple key takeaways. and, i think that you'llfind, if you follow through the instructionson the github page, this was a lot ofinformation thrown at you. it's a whole new framework,but i think it really boils down to a couple key steps.


>> like i mentioned before, settingup the web app is four steps. we went over them before,they're laid out pretty clearly in the getting started page. and basically, anyaction want to implement is kind of, max, three or four steps. >> you usually have to add someitem into the controller in order to tell the app how to handlea certain set of information that gets passed to it, and youhave to edit a view in order to tell your web app whatto do with that information


that it gets passed from the controller. >> and then, dealing with themodel, i think, again, there are a couple of key syntax things. basically, the @items is kindof like a global variable. you can pass it into yourview, which is pretty cool. so, if i go to my viewsfor these things-- "for these things," that was veryspecific-- if i go to my view for, let's say, "items index," you cansee that i can access this items variable if i use ruby code.


so, what that did in thecontroller is, it says, i want to make a global items variable. >> and what i'm going to dowith it, is this sql command. i'm going to go into mytable, get all of the items, and put them all intothis items variable. and then in my view, i'm going touse these items variable, which just has a big list of all of theitems, and this is the for loop syntax we talked about earlier. for each of them, i'm going to printout the title, and the notes about them.


so this is super convenient. if you'll notice, a sql command, twowords, instead of, however many, 15. and, a global variable, allyou have to do is say, @items. >> so, this is a really easy way ofpassing information back and forth from the model, view, and controller. you'll notice, all threeof those, all three parts, are accessed in thesevery simple three lines. this is saying go to the model, orgo to the database, get out data. this is saying, pass it into the view.


>> so, i think that example isactually a pretty nice way of demonstrating why rails is so cool. three lines, we were ableto access our database, get information to our controller,and then pass it back to the view. and again, you'll seea very similar theme for all of the rest of themethods you might want to do. >> you'll see of somereference to the database, and the way of passing it into the view. and, then the views areresponsible for handling


that data in whatever way you want. >> so in the final version, again,i've added a couple more fields to this controller, which allow youto delete, and allow you to edit. the edit looks pretty similar tocreate, because it makes sense, you'll also want to be puttingdata back into the database, like you do when you save a new item. >> delete is a little bitdifferent, because like i mentioned before, it doesn't haveits home page that you visit in order to delete an item.


that'd be kind of annoying, if you hadto go to items/delete/ and then you have to tell it whichspecific item to delete. so, the delete procedureis slightly different, but still follows thesame general conventions. >> it'll have some reference tothe database, where it goes, in order to find the correctinformation and delete it, and it'll have somereference back to the view, so that the view knows what to do. >> so this is ruby on rails.


i would take a look at thegithub, if you're interested. there's a lot of really cooldocumentation setting up users is really easy in rails,compared to almost anything else. there's a gem for itcalled "devise" that takes care of a lot ofauthentication issues, makes a bunch of pages andcontrollers in the background that you just have to set up links to,and your sign-in sign-out functionality is pretty much taken care of. >> it has cool visualization features, ithas really cool, basically everything.


so, i would highly recommend using it. it's super popular these days, too. yeah. if you have any questions, feelfree to email me, or stick around, i'll be here. and thank you so much for coming.


Subscribe to receive free email updates: