Ruby on Rails #47 TIP: create a list of buttons to change status

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello now here is a case what if inside your application you have a table of tasks and each task can have different status like planned in progress and done or you have a list of posts and each post can have a different status like draft published or unpublished well you would want to add a status column to your posts table for example and add a button to be able to change the status and this is what we are going to do in this episode so here i have a list of posts they have a title and content and now we are going to add one more attribute one more column name the status so let's do this i will say rails generate migration add status to posts status it will be a string and going into the migration going to db migrate here we have a string we will say null will be false and by default each post will be a draft so default draft okay and now let's run the migration rails to be my great okay and now if we go to any post we'll go to posts uh show so we will open any post we will add the status to be visible let's do this i will add the post dot status and you see by default the status is draft and will make it an option to be able to change the status well first of all we should list all the available statuses somewhere and they're not going to do it inside the form they're going to make it so that we can change the status directly in the view with clicking a button and this is the interesting part not as a drop down inside the form but just as a button so you click the button and the status changes well we will go to our post.rb and add a list of possible statuses so i will say status or statuses equals and i will say draft then published and unpublished okay and we can actually try to fetch this list of statuses inside our console i will go into the console and say post statuses okay and we can get both statuses dot first or post status says dot 0 or dot 2 so it kind of works and now going back to our post.rb we will actually just in case add an additional validation that validates status presence true okay so now we have a list of available statuses we can actually list the statuses also inside our post i will say equals post statuses let's see what it gives us okay here are the available statuses and we'll make it so that we can press any of the statuses and the status in the database will be updated to the one that we just clicked so for this we'll need to add a link we'll need to add a root we'll need to add a controller action let's start with a root so we're going to go to our config roots and have have resources posts we will say do now we are going to do an action on a single post so we will say member do and we will say patch and we will patch update status okay and now we can add this update status to our posts controller so go into our post controller we will add an action named the update status and as this member do we will be able to and we will have to set the post so we can just add the uh this update status inside out before action or we can go and find the post by params id inside of this update status so we will take post equals post find by params id and we will have some kind of action here we will update the status we'll have something like post dot post dot update uh status and the status will be updated with the status that we pass inside our form so we're going to pass a status with some kind of params well the params are going to be params status and we'll redirect we will redirect to the current post and we'll have a notice status change to the new status so yeah i will make it double like this so that we can pass in uh some code inside we will say status change to at post dot status okay and let's see if this would work well we would need to first of all add this link so we would go and see this new rule that we created let's go to our roots and it would be something that update status okay here we have update status post path and we would need to add this path inside of our link well we would need to have a link so for each status we would add a separate link so going back we have three statuses and for each status we'll have a link we will say post statuses dot each do status okay and we would have an end here then wouldn't have equals here and for each status we would have a link so we would say equals link to then we would have the name of the status then the path would be update status post path then we need to pass in the post so we would say at post and we would need to pass in the status as the params so the params for status will be this status so that when we go to posts uh controller inside of param status we have this kind of value we have a value as chart published or unpublished okay going back to our show view uh let's see yeah we would need to add a method so the method would be a patch method okay and we are closing the braces and now let's see if this works so going back i will refresh and you see we have three buttons i'll press the button to make it unpublished for example and you see status are changed to unpublished and the sales actually was changed to unpublished so it kind of works but we'll make some more additional improvements so what if we want to make the link to the currently selected status unavailable well we would add some tweaks to this link we would say link two unless and we would say it is a link to unless at post dot status dot equals so eql uh params status i think two s so if the status is the one selected then the link will not be available now i will add a comma here and let's see if this works okay it looks like it didn't work so linked unless let's say i will edit without 2s well actually why am i getting from the param status i'm just gonna get it from the status sure so uh if the current status is the one that is in the database then the link is not available and you see we don't need to have this published visible here anymore we can just have a list of all the available statuses and the links will be available to those that are not currently selected and you can have only one selected so see now it kind of works we select a status we make it draft for example save this change to draft and it works and just one final layer of security so just in case somebody tries to tamper with your database if somebody tries to do something like this they will try to update the status to be published let's say the letter two okay i will press published and you see status changed to published tool so it worked i slightly tampered with the database and if i display the post status so the status was changed to publish tool and you see neither of these available statuses is selected so you should not let the hackers kind of do such kind of stuff and you would want to add some kind of additional small layer of security so you would say uh if if params status dot present and if post status is dot includes the status that is provided in the params so if it includes param status and you would add it to sim so it would be a symbol like a draft but not a string like draft then this action would work and otherwise it wouldn't work so if it manages to update we would have status change too and otherwise we would have some kind of alert that you're a hacker and we know it so redirect to post alert stop hacking okay and let's see if it works so i will change the status to draft for example works and now i will go and try to change one of the statuses so i will make it published three and press and you see we get the stop hacking so now it works and basically it is a really kind of cool approach in my opinion to be able to like press a button without going into the form and in some cases just change the status or the progress of something in the application this way you can change the status of a task like it was done or not you can publish and publish a post and do lots and lots of things and the from all the approaches i've seen around the web i think this one is kind of the coolest in this particular domain so anyway i hope you enjoyed the episode and goodbye
Info
Channel: SupeRails
Views: 522
Rating: undefined out of 5
Keywords: ruby, rails, ruby on rails, tutorial, programming
Id: X8kLhQMx1lY
Channel Id: undefined
Length: 12min 28sec (748 seconds)
Published: Sun Jul 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.