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