# Basic search, quick question

**URL:** <https://discuss.rubyonrails.org/t/basic-search-quick-question/4653>\
**Category:** rubyonrails-talk\
**Created:** [November 17, 2006, 3:52am UTC](https://discuss.rubyonrails.org/t/basic-search-quick-question/4653 "2006-11-17T03:52:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Francois\_Montel](https://avatars.discourse-cdn.com/v4/letter/f/65b543/32.png) [@Francois\_Montel](https://discuss.rubyonrails.org/u/Francois_Montel)\
**Post date:** [November 17, 2006, 3:52am UTC](https://discuss.rubyonrails.org/t/basic-search-quick-question/4653/1 "2006-11-17T03:52:30Z")

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

&nbsp;&nbsp;def find\_recipes &nbsp;&nbsp;&nbsp;&nbsp;@recipe = Recipe.find :all, :conditions =\> ['title LIKE ?', "%#{params[:search]}%" ] &nbsp;&nbsp;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:** ![Jimmy\_Kittiyachavali](https://avatars.discourse-cdn.com/v4/letter/j/ecccb3/32.png) [@Jimmy\_Kittiyachavali](https://discuss.rubyonrails.org/u/Jimmy_Kittiyachavali)\
**Post date:** [December 1, 2006, 1:21am UTC](https://discuss.rubyonrails.org/t/basic-search-quick-question/4653/2 "2006-12-01T01:21:03Z")

</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:** ![Jimmy\_Kittiyachavali](https://avatars.discourse-cdn.com/v4/letter/j/ecccb3/32.png) [@Jimmy\_Kittiyachavali](https://discuss.rubyonrails.org/u/Jimmy_Kittiyachavali)\
**Post date:** [December 1, 2006, 1:47am UTC](https://discuss.rubyonrails.org/t/basic-search-quick-question/4653/3 "2006-12-01T01:47:33Z")

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