Codeplace | Advanced Search Form with Ruby on Rails

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome to another stuck cast and today I'm going to show you how to build your own rails search engine so I'm let's start off by showing you the app we're gonna make so here you can see it's just an app that you can search for books you have books in a database and you can search for them and the part let's say they're half of this video is going to be about the simple search or the one that I have here you only you can only search for the name if you type blanket if it turns all the values and the second part is about the advanced search now this is a more robust search engine because it's got his own model so it's let's say that all these fields here are connected are dependent to each other so as you can see if I drag programming and I put it still but nothing okay no records found but if I type Java now I put programming here you go Java get the gory programming okay so I think you will find it very useful to implement in your own website let's begin with creating a new rails app so in the terminal type rails new let's call it book store bookstore it's appropriate okay created all the files needed I was running the bundle now it's completed let's go to run directory CD bookstore near the console okay so has we saw in the demo basically what we need is them it's a table for our books and and we as users must be able to perform some operations so it's the best way to do it is creating a scaffold rails G scaffold book this will create a table named books and we saw that we had the books have names cathegories prices and the ice PA ISBN so let's name the first name string second column category string third prize is a decimal and the last one I speak is BN integer create all the files see here the migration that create the table all the routing controller of course and the views that's migrated okay they will create it now let's check it out now before let's change actually the the route route because we didn't define we haven't find it yet so let's put here the route those two books index okay so let's see what we have so far localhost 3000 okay we have to start it first and okay so I I made a mistake there in the routing it's books not book let's refresh it those fingers that here we go our listing our columns let's create a new one so Java ninja programming will be the category category so um $50 and one two three four created successfully so another one jQuery programming as well yes I make it on purpose I want the same Catherine rate okay 31 four five six seven let's create the last one so firm I don't know D form it will be business the hundred dollars I am the random number okay so they're created so let's let's create the input field here in the natural search form so let's close this app views books index okay I'm going to paste here before my head is very simple one I'm sure you can follow along okay so it's a form tag it submits the books path so if you if you actually see in my premier rake routes you see it here books it's the book index so the path is books the method is get named Serge passes the value for the parameter search that's refreshing okay so whatever I type here notice goes to the URL my diagnosing yeah so it's let's let's put it to work let's actually do something with it so I can actually show the fastest ways put it the condition right here and separated by the comma and here goes the name of the column that you want to compare so named lion custom is the column that you want is the name and between the percent signals you put the hash bracket to construct and you put the params search in there so you're going to compare the name with your parameters if you okay let's test it if you put nothing in return all if you put it low nothing if you put Java every times the Java so it's working already one line of code if you put jQuery sounds as well you put J both okay so it's it is working so this is we cannot put it here because and all the model lodging the database logic should be put on a model so let's clear the class method called search and vesser search parameter here and then let's put an if else condition so if if search exists so if you type act if you actually type something there do something else do another so in here we want to paste this so we want to if you type something we want to compare it against the name column if if not give me all the records so in actually here it's not per am searching search argument up here and in here you put the search and you pass the parameters search okay let's test it gives nothing okay it's a blank if you put below it doesn't find anyone if you put job okay it's working it's already working okay you see this is fine if you want to do simple searches like this one you can go all right with a get method to it through the URL it's it's it's perfectly fine but you want something more complex you should do a model based so I'm going to show you how to do it right now let's create a model just for it I brailles G model search and in here we want to have keywords so I'm going to search for keywords it's a string we're gonna search for category it's another string min price decimal max price decimal 2 and an ISBN which is an integer ok let's let's migrate it ok table search it creative so now let's generate the controller for the searches search of controller remember a plural the model is singular controller is always plural and all right let's create the the drowning let's put resources searches okay same as books okay so let's create the the actions that you need let's go to searches controller I'm just gonna paste here some code that I have copy but basically need three three actions the new creating the show you can see they're just me put it space there okay so new create show the new is straightforward actually this line I'm going to comment and I'm going to explain later the create you create a new pass that function over there and this to show it's a say this is the search parameters where you a private method where you access your attributes in give as an argument to the create function so now let's go here and create a link to the custom search actually I mean put advanced make more sense advanced search this will go to the new search path sorry for the delay okay let's create the actual view so we need the new dot HTML dot GRB and we need the show let's save it show dot HTML dot e RB okay so Donna based here the form for the new it's a simple form as you see I'm going to put advanced here - okay it's a simple form but it has all the fields so it's the keywords the cat three is the min pricey is P n on in this for divs so this is the select I was talking about this is this a normal select option but the values here I set this variable here yet as I said it here what is means this is it's just going to the table books and retrieving all the values from the category but without duplicating so that's perfect for what we want let's actually create I'm going to paste here to the method to actually search show search books I start to assign the books to a book that also if you type nothing it will be all but then I'll go here and go adding conditions if you have typed something so I put it in this way and then return the books the final so if you put something there it will search for that well let me paste it here so the show okay it's based it let me just add a line there so if search dot sort search books in this search books is this method here we created if it's empty output no records found if it's not empty then for each result output the name price category and ISBN inside this little live here on the records so now let's try it okay if I typed hello no records found so it's working need file let me see Java if it reaps returns Java so it's working to programming's that the times room rooms the purpose of the whole cats were okay so I made a mistake there let me talk and correct it if I go there now I can just only one programming research okay it runs to results so it says it's working let's try the ISBN it it's working yeah see it's let me try another one no records far huh so you see you have a pretty decent search engine I mean also for them the price as well so you have a pretty decent drip rails engine here with a simple form in the in the in the first one in the first page is just a very simple one just searches for the one column but then the advert advanced search which I think is ready to implement in in your website so this is all there is to it and I hope you'd find that the I hope you found it useful and see you again in another step cast
Info
Channel: Codeplace
Views: 38,862
Rating: undefined out of 5
Keywords: codecast, ruby on rails, rails, stuk, stuk.io, search, form, engine, search engine
Id: eUtUquKc2qQ
Channel Id: undefined
Length: 13min 56sec (836 seconds)
Published: Tue Jan 13 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.