Hello I created a search engine on my site but it only works when you type the full name of the string. My string name are on average 9 characters long. I would like to be able to search one word of the string and then get the listings to show up that have what you searched in it. This is what I have: _________________________________________________ home.html.erb <% form_tag "/home/find", :method => ''get'' do %> <%= text_field_tag :search, params[:search] %> <%= submit_tag "Search", :name => nil %> <% end %> _________________________________________________ home_controller def find @deals = Deal.find(:all, :conditions => ["item_title = ? OR description = ?", params[:search], params[:search]]) end -- Posted via http://www.ruby-forum.com/.
Aaron Day wrote:> Hello I created a search engine on my site but it only works when you > type the full name of the string. My string name are on average 9 > characters long. > > I would like to be able to search one word of the string and then get > the listings to show up that have what you searched in it. This is what > I have: > > _________________________________________________ > home.html.erb > > <% form_tag "/home/find", :method => ''get'' do %> > > <%= text_field_tag :search, params[:search] %> > <%= submit_tag "Search", :name => nil %> > > <% end %> > > _________________________________________________ > home_controller > > def find > @deals = Deal.find(:all, > :conditions => ["item_title = ? OR description = ?", > params[:search], params[:search]]) > endHey - check out http://www.tizag.com/mysqlTutorial/mysqlwhere.php You can use wildcards in your mysql queries. An example in rails would be: Deal.find(:all, :conditions => ["item_title LIKE ?", "%#{params[:search]}%"]) Also - why have you set the value for the text_field_tag to params[:search]? Gavin http://handyrailstips.com -- Posted via http://www.ruby-forum.com/.
> Deal.find(:all, :conditions => ["item_title LIKE ?", > "%#{params[:search]}%"]) > > Also - why have you set the value for the text_field_tag to > params[:search]? > > Gavin > > http://handyrailstips.comThe params[:search] was from: http://railscasts.com/episodes/37-simple-search-form I tried to go by that at first. And a book I have told me to use params[:search_string]. I also tried messing with the item LIKE ? the other day but didn''t get to far but I''ll try messing with it again. I''ll post again after I checked your link and try a few other things. Thanks -- Posted via http://www.ruby-forum.com/.
I alright I think my problem was that I was using to sources of different information. I used: :conditions => ["item_title LIKE ?", "%#{params[:search]}%"]) and it worked. Thanks Gavin for the help! -- Posted via http://www.ruby-forum.com/.
You''re welcome :) I see what you''ve done with the params[:search] This is used to show the search term in the text-box on the results page. This seems perfectly reasonable Gavin http://handyrailstips.com Aaron Day wrote:> I alright I think my problem was that I was using to sources of > different information. > > I used: > :conditions => ["item_title LIKE ?", "%#{params[:search]}%"]) > and it worked. > > Thanks Gavin for the help!-- Posted via http://www.ruby-forum.com/.