How to Use Collection Select in a Rails Application

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi in this video we're going to extend what we learned last time in learning how to create a drop-down form box here in a rails application and we're going to actually create a database call that queries one of our database tables a new one that we're going to create in this episode and we're going to do it for the salesperson a slot right here so that you'll actually be able to click on this and it will call a employee database and you'll be able to pick which salesperson is associated with this invoice and so the way that we're going to start this out is we'll come back to our application and as always we'll start off creating a branch so go get checkout and we'll just say add DB form call a little bit longer than normal but I like them to be relatively descriptive especially if we would be pushing this branch out for working with the team so that is set and now what we want to do is we want to create our employee model and our employee database table so to do that we've done it before but just as a review you're going to do rails G scaffold which will create all the necessary items that we need and we're gonna say we're going to call this one just employee make sure employees capitalized and then we're going to have a name which is a string and then let's see we should have things like a phone number which will actually call this one a it's called an integer nevermind let's call a string so that we can include other things like parentheses and those kind of things in it so that would be a string and then the employee type or job type job underscore type which should be a string that's how we're going to save someone's a salesperson or a manager something like that and that's really all we need for this one and we also want to make sure we do - - no - style sheets because what that's going to do is we're not going to create a ton of new style sheets with this which would kind of get in the way and this should be all we need so we're going to return and have that scaffold work so that we can get this new table created and that looks like it all worked now I'm going to go up into the application and just to look at it make sure everything looks good so click on DB migrate and then we'll see that it's about to create employee and it's got a name which is a string phone string and job type which is a string this is a nice and easy one so get out of that and then just do rake DB migrate hit return and the database has been migrated so that's all working so now let's start up the application just to make sure that we have everything we need and I'm also going to open up a new tab here in the rails console and take a look at our routes which if you're getting familiar with rails you should already know what we're about to see here but it's no problem if it's still still kind of fuzzy that takes a while so click rake routes and we'll see the new routes that we have available for employee ok so scroll up ok to go see the employees we're just going to go slash employees to create a new employee we'll do slash employees slash new and all those other ones so and we know that our all of our routing is working so that's good and looks like the server's going come here hit refresh and come up and do employees okay so that's working I'm just going to add a couple fake ones in here and so go and I'll show you why I'm doing this in a second and we'll say Tiffany's a manager and we will format this but not at the moment okay so we have a list of three different employees right here and if we want to go to invoices next thing we're going to do is create a drop-down that actually queries that so come back in the application and you don't have to get rid of the server because that's going to still keep running we're not gonna have to restart it and now just go to app and go to views and go to invoices and then we want this to be in our form and we already have sales person and you can see it's right here but this is just regular form field and so we want to get rid of this so go from div to div and now what we're going to do is create something relatively similar except it's going to be another collection sort so we're going to do div class equals control group to our embedded Ruby so to F label salesperson class control group and I definitely get used to the syntax so if it's still not making perfect sense practice it quite a bit because that's how I did it and it really just takes a lot of repetition if you are picking up on a quick thing congratulations then you're one of the lucky ones but don't worry if you're not so now some more embedded Ruby and this time we're going to do instead of F dot select we're going to do collection select and this is where we're going to put our database call okay so the very first thing we have to do is actually call our invoice so go invoice and and from here we're going to do something called sales person ID which does not exist right now so if it if you're wondering how we're doing that it's we're gonna have to add this into the invoice table so but you'll see how we do that here in a second so sales person ID then we're gonna do sales person all ID name a beginning and ending curly brackets comma and then start curly brackets multiple hash rocket false and the curly brackets in the parentheses and then in the embedded Ruby and then just close out our DOS fix the indentation okay and this is all well and good but the one thing you will notice if we go down to dB and then go to schema and go down to invoices on invoices you'll notice that we actually have sales person here and it's is expecting a string which is not good what we actually want is for this to become an integer and we want sales person to read sales person ID and so if you're familiar with sequel in the way that you script different relational database tables then this will make a lot of sense to you if not then do not worry at all we'll walk through it very slowly and you'll be able to understand exactly how it works so the very first thing we're going to do to make this refactor happen is is come down to the console and exit the the rails server and we want to do a database migration so we'll do rails G migration and just type in a remove salesperson from invoices and we're not going to pass at anything because we're going to do this whole manually which is good practice you could put some more things in here to put this information in but I actually want to show you how to do this by hand so hit return it will create a migration file for us but as you've seen in other times where it put a lot of content in it for us this time it's going to be blank so you can see that it doesn't actually put anything in here which is perfectly fine this is what we're going to do so we're just going to remove column and we want to remove it from invoices and we want to remove salesperson make sure everything is spelled correctly yes that's good and it's a string and then in here we can also do our add column so we can kind of multitask so we want to add column and do invoices and then salesperson ID and we want this to be an integer okay and we also could do this as a reference but to keep it simple we're just gonna do it this way and we'll create our own references manually so okay just want to review this and make sure your migrations are always accurate and you don't have a spelling mistakes or else they can get kind of messy so I'm going to remove a column which is salesperson and we want to add a column which is salesperson ID so hit save and come back down the console and you rake DB migrate perfect so now if we open up our DB schema file and come down to invoices you can see now we have salesperson ID right here which is fantastic so this is all working but before it can actually really work we have to go and go to our models and then go to invoice and we're going to type in a belongs to salesperson okay so this should work and all this is saying is that the invoices essentially belong to a salesperson because the salesperson is going to be associated with it and this is going to allow us to do some nice connections on the relational database side so we'll close out of here and close out of here and let's cross our fingers and see if this is going to work so we'll start up the server that work that didn't break yet and then come here and hit refresh oh and I didn't like it okay what didn't it like name or initial uninitialized constant compiled time salesperson okay oh and this was a stupid mistake come back scroll back down to your views go Oh to form we don't have a model called salesperson this is called employee sorry about that and you know what I just realized we are going to have to fix our migration too because salesperson is a network this has to be an employee ID so come here come back down to the console and I would just edit this out of the video except I can tell you from personal experience this is going to happen to you on a regular basis it's don't feel bad when it happens and I'd rather keep this on here so you can know exactly what to do when you get these type of errors so I'm going to see okay and let's yeah let's do a migration again so I'll do rails G migration remove sales person ID from invoices okay go back down to DB go to the latest one oh and it looks like we need to refresh cuz it there we go okay so come here and I'm essentially just going to copy this because this is exactly what we need just with a few words swapped out so we want to remove salesperson ID and swap it out with employee ID okay and this is an integer hit save okay now let's break D be migrated okay rails start up the rails server okay now come back to the site hit refresh and it's working and as you can also see this is making a perfect call right here to our employee database and it looks like our salesperson is a little screwed up so let me go and fix that and this isn't working because we need to change this because we don't have a salesperson anymore so it save come back hit refresh and we still have a little issue with our CSS oh and you know it's not our CSS it's actually this means we control label save come back refresh and there you go it's all working perfectly so this is pretty neat now what we have is a fully functioning kind of system we still have to add the ability to add line items obviously because it's an invoice however the neat thing you can do now is we can come you see we have Jordan Tiffany and David if we want to come up and do things like add a new employee and now we can add Christine and job type salesperson great employee come back to invoices hit new invoice and there we go we can see that we now have Christine added to the bottom of the list however it won't work yet and the reason it won't work is because what we've discussed before without rails uses strong parameters so we can't just expect to add a bunch of new things in the model and the table and the form to know what to do and so to fix that we'll have to come back here come up to controllers go to our invoices controller go down the bottom and instead of salesperson we want to employee underscore ID and then if we go down a little bit to their views invoices and index we don't want salesperson and we don't want this either I'll show you we're about I'm going to do something just to show you that it works I'm going to do employee ID this is going to call the actual element but it's not what we want it's only going to show the the employee number but we'll come back here hit new invoice and I'll pick out say the 13th we'll do XYZ widgets and I'll pick out Tiffany for the employee paid submit and now you can see that we have this new line item and it has sales person and you can see it says two right here and if you're wondering why it says two this is the employee ID it's not actually the employee name you get the employee name or the salesperson name that's what our next video is going to be about and it's going to be a lot more about sequel queries and filtering and database relationships because that's going to be something extremely important and it's definitely too long to just kind of throw on at the end of a video without making another 15 20 minutes longer so our next video is going to be dedicated to that and showing how to build those relationships with the databases so that you can use one query to call multiple tables and then put the data together so it all looks like it's one to the end-user so congratulations it went if you went through this video and follow it along with all the steps you now know how to create a collection form element and how to use it to query a completely different database table and display that as an option to users and then to pass that into a new into a new database entry and you also saw how to do a number of different database migrations to remove columns add columns and also how to do some preliminary work in setting up relationships between tables so good job and please let me know if you have any questions whatsoever and I'll see you in the next video
Info
Channel: edutechional
Views: 25,084
Rating: undefined out of 5
Keywords: Ruby On Rails (Software), collection select, rails form helpers, drop down form input, database query in form, form tips, html drop down, database migrations
Id: rf6B9oo1zPY
Channel Id: undefined
Length: 19min 45sec (1185 seconds)
Published: Mon Apr 14 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.