# Form\_for search question

**URL:** <https://www.ruby-forum.com/t/form-for-search-question/148555>\
**Category:** Rails\
**Created:** [September 14, 2008, 10:34pm UTC](https://www.ruby-forum.com/t/form-for-search-question/148555 "2008-09-14T22:34:10Z")\
**Posts on this page:** 3\
**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:** [September 14, 2008, 10:34pm UTC](https://www.ruby-forum.com/t/form-for-search-question/148555/1 "2008-09-14T22:34:10Z")

</div>

Hi there,

I’m going through this tutorial on Thinking Sphinx:

> **[\#120 Thinking Sphinx - RailsCasts](http://railscasts.com/episodes/120-thinking-sphinx)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

I’m really new to searches in rails; how do I get the search results to  
display on a separate search result page?

I have a search form that appears on every page (in my  
application.html.erb template) that looks like this:

\<% form\_for :recipes, :url =\> {:controller =\> “pages”, :action =\>  
“searchresults”} do |f| %\>  
\<%= f.text\_field :search %\>  
\<%= submit\_tag “Search” %\>  
\<% end %\>

This is a recipe search, but it doesn’t even work because :search isn’t  
a field in the recipes table. I just want to grab a params[:search] that  
I can use in my controller.

Here’s what my pages\_controller.rb looks like:

def searchresults  
@recipes = Recipe.search params[:search], :page =\> params[:page],  
:per\_page =\> 10, :field\_weights =\> {:title =\> 20}, :match\_mode =\>  
:boolean  
end

I have no problem with this code; I have a searchresults.html.erb that  
matches up just fine. But params[:search] == nil, so that’s no good. ☹

How should I construct my form?

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:** [September 14, 2008, 11:02pm UTC](https://www.ruby-forum.com/t/form-for-search-question/148555/2 "2008-09-14T23:02:15Z")

</div>

On Sep 14, 9:34 pm, Dave A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I have no problem with this code; I have a searchresults.html.erb that  
> matches up just fine. But params[:search] == nil, so that’s no good. ☹

Check params again. you search is there, just not where you think  
(hint: if you put \<%= debug params %\> in the view, that will dump all  
the params to the screen. alternatively just look in development.log.

Fred

---

<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:** [September 14, 2008, 11:32pm UTC](https://www.ruby-forum.com/t/form-for-search-question/148555/3 "2008-09-14T23:32:41Z")

</div>

Frederick C. wrote:

> On Sep 14, 9:34ï¿½pm, Dave A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > I have no problem with this code; I have a searchresults.html.erb that  
> > matches up just fine. But params[:search] == nil, so that’s no good. ☹
> 
> Check params again. you search is there, just not where you think  
> (hint: if you put \<%= debug params %\> in the view, that will dump all  
> the params to the screen. alternatively just look in development.log.
> 
> Fred

Thanks! I switched to a form\_tag because I wasn’t querying the model.  
Here’s what I came up with:

\<% form\_tag :action =\> ‘searchresults’ do %\>  
\<%= text\_field\_tag :search %\>  
\<%= submit\_tag “Search” %\>  
\<% end %\>
