search issue

Hi all. I have a web app where by people submit data to the DB. I have a page that lists the last 5 submissions by the ID number. This allows a user to click one of the last 5 and it brings up all the data for that ID. e.g. /posts/121314 Id being 121314 for example. This all works fine. However I have a search page which is just a box which asks the user to either search the ID if known or company name by entering it in a form_for helper box. This bit I am having soo much trouble with.

For example, ideally if a user enters the id number in the box, lets say it's 12345 it routes directly to /posts/12345 as this will show the data.

Also I need that when they enter the company name it searches the db and then lists all entry id's that also have this name assigned to them. Once that bit is done I can continue with making them link to the pages etc...

so i Have a search page within my posts controller as below and when they enter on the form more so for the company name it send it to the 'searchshow' page to lists the entries. Both these are currently empty.

    def search

    end

    def search show

    end

any help or pointers in the write direction will be great. I have seen lots of info regarding search etc but they don't really cover my needs. I am new to ruby and rails really as you can tell. :slight_smile:

I would start by TDDing a class/method that will take your various expected inputs and tell them apart. Then use the output of that to generate the appropriate 'where' statement to select what you want from the DB (assuming you don't need anything too sophisticated, e.g. fuzzy search, stemming, etc.).

After that, easy-peasy, it's just redirects and views.

HTH,

to layout what Hassan wrote a bit. First, you should add a resources route to config/routes.rb

resources :search

and create a SearchController

type

rake routes

into the terminal in the root of your application to see which routes you will have available with the above setting.

form with input field on your search page → The user is giving a string with e.g a ID

Note: you should inform the user what format you are expecting: e.g. a string consisting solely of numbers

Because you are not implementing a full search here → submit the form

now check out the simple implementation here: https://gist.github.com/andywenk/a1fa786615a16c08499f

I hope this helps to get the idea. Checkout the Rails guides for in depth info at Rails Routing from the Outside In — Ruby on Rails Guides

Cheers

Andy

hi guys .. this is extremely helpful. Many Thanks