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
Aaron D. 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]])
end
Hey - check out MySQL Tutorial - Where
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
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
The params[:search] was from:
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
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
Aaron D. 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!
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!