search issue

74 views
Skip to first unread message

Mark Mark

unread,
May 11, 2014, 1:01:46 PM5/11/14
to rubyonra...@googlegroups.com
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. :)

--
Posted via http://www.ruby-forum.com/.

Hassan Schroeder

unread,
May 11, 2014, 2:14:50 PM5/11/14
to rubyonrails-talk
On Sun, May 11, 2014 at 10:01 AM, Mark Mark <li...@ruby-forum.com> wrote:

> 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.

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,
--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan

Andreas Wenk

unread,
May 12, 2014, 5:37:39 AM5/12/14
to rubyonra...@googlegroups.com
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 http://guides.rubyonrails.org/routing.html#resources-on-the-web

Cheers

Andy

Mark Mark

unread,
May 14, 2014, 4:08:18 PM5/14/14
to rubyonra...@googlegroups.com
hi guys .. this is extremely helpful. Many Thanks
Reply all
Reply to author
Forward
0 new messages