# Basic search, quick question

**URL:** <https://www.ruby-forum.com/t/basic-search-quick-question/76268>\
**Category:** Rails\
**Created:** [November 17, 2006, 3:25am UTC](https://www.ruby-forum.com/t/basic-search-quick-question/76268 "2006-11-17T03:25:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [November 17, 2006, 3:25am UTC](https://www.ruby-forum.com/t/basic-search-quick-question/76268/1 "2006-11-17T03:25:46Z")

</div>

Hi, I know there are other threads about this topic, but I’ve checked  
and none seem to help my problem.

I’m trying to get a simple search to work on my Rails app, but I get a  
“Couldn’t find Recipe without an ID” error when I search. Here’s what I  
have:

list.rhtml

\<%= start\_form\_tag :action =\> ‘find\_recipes’ %\>  
  
\<%= submit\_tag ‘Search’ %\>  
\<%= end\_form\_tag %\>

recipes\_controller.rb

def find\_recipes  
@recipe = Recipe.find :all, :conditions =\> [‘title LIKE ?’,  
“%#{params[:search]}%” ]  
redirect\_to :action =\> ‘results’  
end

def results  
@recipe = Recipe.find(params[:title])  
end

results.rhtml

\<%= @recipe.value %\>

Is it something obvious and dumb? Also, how do I get it to search  
several fields at the same time?

Thanks!

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [November 17, 2006, 3:32am UTC](https://www.ruby-forum.com/t/basic-search-quick-question/76268/2 "2006-11-17T03:32:54Z")

</div>

Hi –

On Fri, 17 Nov 2006, Dave A. wrote:

> \<%= start\_form\_tag :action =\> ‘find\_recipes’ %\>  
> end
> 
> def results  
> @recipe = Recipe.find(params[:title])

It doesn’t look like you’ve set params[:title] anywhere. I also  
suspect you don’t really want :title here.

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [November 17, 2006, 4:53am UTC](https://www.ruby-forum.com/t/basic-search-quick-question/76268/3 "2006-11-17T04:53:03Z")

</div>

in your find\_recipies controller you’re looking for a recipe based on  
params[:search] (the value submitted in the text box), and setting it  
to the variable @recipe. But then you redirect to the results method  
which then runs find again, this time based on params[:title] which  
doesn’t exist, and then storing that to @recipe, which is then  
displayed.

The simple fix here is to drop your results method altogether

recipes\_controller.rb

def find\_recipes  
@recipe = Recipe.find :all, :conditions =\> [‘title LIKE ?’,  
“%#{params[:search]}%” ]  
end

find\_recipes.rhtml

\<%= @recipe.value %\>

(by the way, you can also use the text\_field helper instead of the html  
input tag in your view.rhtml).

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [November 17, 2006, 6:19am UTC](https://www.ruby-forum.com/t/basic-search-quick-question/76268/4 "2006-11-17T06:19:21Z")

</div>

Hi, great, that makes much more sense to me. I’m still getting an error  
(undefined method ‘value’). What do I have to do to make .value work?

Also, if I want it to search more than titles, like also descriptions,  
how would I add that in there?

Thanks for all the help so far; I’m learning a lot.

FranÃ§ois Montel wrote:

> in your find\_recipies controller you’re looking for a recipe based on  
> params[:search] (the value submitted in the text box), and setting it  
> to the variable @recipe. But then you redirect to the results method  
> which then runs find again, this time based on params[:title] which  
> doesn’t exist, and then storing that to @recipe, which is then  
> displayed.
> 
> The simple fix here is to drop your results method altogether
> 
> recipes\_controller.rb
> 
> def find\_recipes  
> @recipe = Recipe.find :all, :conditions =\> [‘title LIKE ?’,  
> “%#{params[:search]}%” ]  
> end
> 
> find\_recipes.rhtml
> 
> \<%= @recipe.value %\>
> 
> (by the way, you can also use the text\_field helper instead of the html  
> input tag in your view.rhtml).

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [December 1, 2006, 2:24am UTC](https://www.ruby-forum.com/t/basic-search-quick-question/76268/5 "2006-12-01T02:24:16Z")

</div>

I assume this is the code you have running now:

`
list.rhtml
<%= start_form_tag :action => ‘find_recipes’ %>

<%= submit_tag ‘Search’ %>
<%= end_form_tag %>
recipes_controller.rb
def find_recipes
@recipe = Recipe.find :all, :conditions => [‘title LIKE ?’,
“%#{params[:search]}%” ]
end
results.rhtml
<%= @recipe.value %>
`

So the key thing to note here is that Recipe.find(:all, …) returns an  
array of Recipe objects. So your instance variable would properly be  
named  
@recipes. In your results.rhtml, you’d want to do something like

\<% @recipes.each do |recipe| %\>  
\<%= recipe.value %\>  
\<% end %\>

If you simply want the same search field to search both title and  
author,  
then you’d have to do something like :

def find\_recipes  
@recipe = Recipe.find :all, :conditions =\> [‘title LIKE ? or author  
LIKE  
?’,  
“%#{params[:search]}%”, “%#{params[:search]%” ]  
end

But that is getting messy, and is not quite DRY. Though I haven’t used  
it  
myself, I hear good things about the acts\_as\_ferret extension.

Good luck.

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [December 1, 2006, 12:40am UTC](https://www.ruby-forum.com/t/basic-search-quick-question/76268/6 "2006-12-01T00:40:05Z")

</div>

I still haven’t figured out how to get the search results to display in  
the view file find\_recipes.html. I can’t seem to get around the  
“undefined method ‘value’” error.

I’m searching in the title, but I want to bring up more than just the  
title in the search results (things like the author). Is that much  
harder to implement?

Thanks!

Dave A. wrote:

> Hi, great, that makes much more sense to me. I’m still getting an error  
> (undefined method ‘value’). What do I have to do to make .value work?
> 
> Also, if I want it to search more than titles, like also descriptions,  
> how would I add that in there?
> 
> Thanks for all the help so far; I’m learning a lot.
> 
> FranÃ§ois Montel wrote:
> 
> > in your find\_recipies controller you’re looking for a recipe based on  
> > params[:search] (the value submitted in the text box), and setting it  
> > to the variable @recipe. But then you redirect to the results method  
> > which then runs find again, this time based on params[:title] which  
> > doesn’t exist, and then storing that to @recipe, which is then  
> > displayed.
> > 
> > The simple fix here is to drop your results method altogether
> > 
> > recipes\_controller.rb
> > 
> > def find\_recipes  
> > @recipe = Recipe.find :all, :conditions =\> [‘title LIKE ?’,  
> > “%#{params[:search]}%” ]  
> > end
> > 
> > find\_recipes.rhtml
> > 
> > \<%= @recipe.value %\>
> > 
> > (by the way, you can also use the text\_field helper instead of the html  
> > input tag in your view.rhtml).

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [December 1, 2006, 2:50am UTC](https://www.ruby-forum.com/t/basic-search-quick-question/76268/7 "2006-12-01T02:50:23Z")

</div>

I apologize,  
This was a cut-and-paste error on my part. Make sure that in your  
controller, you are setting the @recipes variable (not the singular  
@recipe). So the controller should be something like:

def find\_recipes

# the @recipes will be an array (possibly empty) of Recipe models

@recipes = Recipe.find :all, :conditions =\> [‘title LIKE ?’,  
“%#{params[:search]}%”]  
end

Your view should be something like :

\<% @recipes.each do |recipe| %\>  
\<%= recipe.value %\>  
\<% end %\>

Note that the @recipes in the view is set by the controller. I assume  
that  
the error about calling nil.each you encountered earlier is because in  
the  
controller, you set @recipe, and in the view, you used @recipes.

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [December 1, 2006, 3:27am UTC](https://www.ruby-forum.com/t/basic-search-quick-question/76268/8 "2006-12-01T03:27:35Z")

</div>

Yes, thank you so much! That’s it! 🙂

Jimmy Kittiyachavalit wrote:

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [December 1, 2006, 2:40am UTC](https://www.ruby-forum.com/t/basic-search-quick-question/76268/9 "2006-12-01T02:40:01Z")

</div>

Thanks for your help, but I still can’t get it to work. ☹

When I try to do it just as you have it, I get an error trying to  
evaluate nil.each, which makes me think that Rails couldn’t find  
anything in the @recipes variable in results.rhtml. Does this have to  
match @recipe = Recipe.find… in the controller?

When I change @recipes to @recipe in results.rhtml, it tells me that it  
doesn’t recognize the method “value”. Is this a standard part of Ruby,  
because I don’t mention it anywhere in the controller.

Sorry for all the questions, I want to make it work and understand how  
it works at the same time. Your explanation before was great!

Thanks again,

Dave

Jimmy Kittiyachavalit wrote:

> I assume this is the code you have running now:
> 
> `
> list.rhtml
> <%= start_form_tag :action => ‘find_recipes’ %>
> 
> <%= submit_tag ‘Search’ %>
> <%= end_form_tag %>
> recipes_controller.rb
> def find_recipes
> @recipe = Recipe.find :all, :conditions => [‘title LIKE ?’,
> “%#{params[:search]}%” ]
> end
> results.rhtml
> <%= @recipe.value %>
> `
> 
> So the key thing to note here is that Recipe.find(:all, …) returns an  
> array of Recipe objects. So your instance variable would properly be  
> named  
> @recipes. In your results.rhtml, you’d want to do something like
> 
> \<% @recipes.each do |recipe| %\>  
> \<%= recipe.value %\>  
> \<% end %\>
> 
> If you simply want the same search field to search both title and  
> author,  
> then you’d have to do something like :
> 
> def find\_recipes  
> @recipe = Recipe.find :all, :conditions =\> [‘title LIKE ? or author  
> LIKE  
> ?’,  
> “%#{params[:search]}%”, “%#{params[:search]%” ]  
> end
> 
> But that is getting messy, and is not quite DRY. Though I haven’t used  
> it  
> myself, I hear good things about the acts\_as\_ferret extension.
> 
> Good luck.
