The Rails Params Hash Explained

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the params hash in your rails application is a concept that a lot of people have trouble with so I'm going to talk about it and try to debunk the magic that sort of happens with it because it's actually really simple the params hash is a collection of data that's come through your application in that request so this data can come from various places you might submit a form and that sends over some data your URL might contain a chunk of the URL that has some data that you want and you also might have at the end of the URL get request parameters so those are the common ways to get data into the params hash and rails just knows to collect those things and put it in there nicely for you to work with so let's talk about how we go about doing that the very simplest thing that we can take a look at is get request parameters so let's open up our blog application and jump into our routes our B file so I'm gonna clear out these comments and add just a simple simple route and we'll call it slash blog and I'm gonna send it to the blog index action I don't have a blog controller yet or an index action so we'll need to create that real quick so let's do that edit app controllers blog controller dot RB create a class called blog controller inherits from application controller and it has an index so that's as simple as that is I'm going to make a directory and app views call it blog and then we'll edit the app views blog index.html that your B file and we'll just say vlog so when we visit the slash blog route when we run our rails application we will see that file so we'll run our rail server and then open up in our browser localhost 3000 slash blog and this will show us the blog path so we got the view that we created and we're able to see it and you can see in our logs that we have get slash blog that's all working correctly now a get request parameter is what you might have seen before and like the Google URLs and different things around the web where there's a question mark and then there is a variable and it's just like something like test equals one so you pass that in the page request is actually localhost 3000 slash blog but then this extra information our query parameters so these query parameters basically say well here's some extra data you might use this to render your page you might not you can totally ignore it just like this page did it didn't it didn't blow up or anything because it it can ignore this information unless you want to use it so if we look at our rails logs again now we have parameters and the parameters say that test equals one and in our previous requests get slash blog didn't have any parameters so rails is taking these parameters and pulling them out now when it writes the word parameters and gives you this hash this is actually exactly what's in your params hash so when you go into your controller blog controller rather when you go in here if you have the word params that's accessing this variable so they show you exactly what's there so you can work with it so this is not magic by any means when you say params test that is the test variable so this is going to pull the string of one out and if we say at test and pass this into our view we can go to blog index and print out at test here and when we show our page we can see that it's one if we do two it changes so what this is doing is its reading from the params hash the test variable and pulling out the value for that key the test key and then we're going to just save it in a variable so you can use the square brackets and a symbol to match the name and that will return the correct value so it's really really simple forget parameters to be doing that to pass into the params hash and there you go you have data so this is really useful when you want to say blog page three blog page two and so on this allows you to sort of filter out what the page displays you're not really changing what this page does but you might filter out a little bit of how the page works so these are really good for filters and that is a get request parameter so the next one we'll take a look at is the route parameters so if we go back to our routes file and we add a new route and we say slash blog slash ID now we put the colon here because this is it's just basically telling rails that the the part that's typed into the URL should be taken and saved into the params hash as the ID name so when we say we want to view individual blog posts so we have a show action and with this URL we can take that stuff that's passed in in that section after a blog save it to the ID key inside of the params hash and then go look it up so if we go into our blog controller and we add a show and let's just not actually do a lookup in the database let's just make this very simple so we'll have here's my blog post so when we visit anything / blog slash absolutely anything you see here's my blog post you can put in and thing you want so you can put in numbers you could put in words you name it they will get passed in to your rails application so now you can see that the parameters have the ID key in value and ID now is asdf and when we passed in one it was one so you can use this basically to say okay in this case we don't want the user to put in ID equals one or asdf we actually want the URL to look prettier so we want it to be more purposeful and this tells the browser and Google and other things that this is a different page there's something else that they're looking at for every one of these and when you add a query parameter on there like page equals one the search engine knows that you're still on the blog like the index page but you're just looking at a few other different posts on that page so it's still generally the same page they've just decided to split it up and when you do separate URLs these signify that there's separate pages and they're completely different so this is good for us for when we pass in the ID or the name of a blog post we can just do a look up in our database with active record pull that out and display it on the page so you can edit those or access those rather in your blog controller the exact same way so if you want params ID which you'll see often in your rails examples if you have a blog post model which I haven't created one you could find one though doing this and that would just load up your params ID number into the fine method and that will go look up the database record based upon the number and the URL so by default to use numbers eventually you'll probably change them over to using words and looking by slugs but we can see this just by saying that let's name a variable called ID and an instance variable and then we can print it out here on the page as well just like we did before with tests so in this case what did I miss I didn't save that file so in this case we see number 1 we can change this to a SDF and so on now here's something interesting we can take and have multiple ones so you could have test in here as well and you could have the test query parameter in there too so you could have test equals 1 and if we decide to print out at test we can see both of them so these are both filtering in rails knows that okay if they come in from a query parameter we should put that in and if they come in from a route parameter we should put those into the params hash as well and you can see the parameters there now test is 1 and ideas SDF so they're both being funneled into that which is awesome last way we get data into our application params hash is by submitting data through some sort of other requests so we've talked about get requests so far and you can put stuff in the URLs and that's pretty much how you get data into a get request now post patch update and delete requests are all designed so that you submit data to the server and they will take that and automatically put it into the params hash so rails knows to basically use these three things we're going to use an example of a new blog post so we're going to say that if you post to the blog URL we're going to go to the blog create action so this should actually create a blog post or at least that's our intention what we need now is to create a form that submits a post request to this URL and I'm gonna do this on the blog index so we're just gonna create a regular form here for the blog path and we're going to we're going to create a submit tag so we have a button that says create post and before that I think we should have a text field tag for the title and we should also have a text area tag for the body if we refresh the blog page now we have a title field and a body field I didn't put labels on them because it's simple and we are just submitting data over to the server so if we actually just call these let's call it post title and post body so when we create this post or submit the form it's going to send a post request and we're going to get unknown action so first thing we need to do is go into the blog controller create an action call create and I'm just gonna have it redirect to the index action so it's not going to do anything we're gonna be able to see the requests in the logs though when we really submit it so I'll come back to the same page and we'll be able to look at the logs to see what's happened now this is our post request to the blog path and went there it created it it created the post request submitted data just like we set it to and when we created the the form here and I just said title and body I made those up so I wanted to have a params value of title and a params value of body and that's that's what it did you just give these names so ideally with rails you make these names match what's in your database models so that it can automatically match them up and you don't have to do it yourself but for this example we're just submitting over data and you can see now that the names are going to affect how it shows up in the params so if we said post title here we can see that if we were to refresh this page and put in different text in our logs this time we will see that post title comes across so you are free to name these anything you want the utf-8 and authenticity tokens are automatically included with Rails form tags and they allow it to basically force your browser into using utf-8 because they use a check marks check mark utf-8 character and then the authenticity token is too awhile CSRF protection basically to keep your forms a little bit safer so the ones that we're interested in are the ones we submitted and the commits here is actually the text for the the button that we created so if you actually need that information you can you can see that the value of a commit button or submit button is going to be the text that's displayed so that allows you to actually do stuff based upon the button that that is submitted if you need it it's unlikely you probably do but it's there in case you do so submitting data in a form is really very simple it's very similar to what the other ID URL things and the query parameters are so you're just naming things whatever you want to name them submitting it to the server and rails knows those locations to pull them out and then cleanly organize them in your params hash so every time that a request comes over and you're missing data or something check what params are listed here so that you can see that it really did come over as maybe you missed it or maybe it's ignoring it after it came over so really came over but you did something wrong in your code so the the way that the params hash is actually very simple there's the three typical methods of data coming into your server and that just aggregates all of them together so generally try not to duplicate names you don't want you don't want to have body in the URL and in your post form so you you will probably get something overridden and that will be bad so don't duplicate the names but aside from that there's there's really nothing to worry about this is very simple and you can access all of these variables really easily
Info
Channel: GoRails
Views: 20,604
Rating: undefined out of 5
Keywords: Ruby On Rails (Software)
Id: y57OnWV6dRE
Channel Id: undefined
Length: 15min 51sec (951 seconds)
Published: Sun May 17 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.