# Help on Simple Search

**URL:** <https://www.ruby-forum.com/t/help-on-simple-search/50684>\
**Category:** Ruby\
**Created:** [December 19, 2005, 11:25pm UTC](https://www.ruby-forum.com/t/help-on-simple-search/50684 "2005-12-19T23:25:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Grant\_GBrown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9de0a6/32.png) [@Grant\_GBrown](https://www.ruby-forum.com/u/Grant_GBrown)\
**Post date:** [December 19, 2005, 11:25pm UTC](https://www.ruby-forum.com/t/help-on-simple-search/50684/1 "2005-12-19T23:25:03Z")

</div>

Hey there,

I realize this is simple but I am unable to find a working example  
anywhere. I just need an example of how to submit a search value from a  
form, and have a query run on that value using Rails. For example:

BASIC form…one field. Enter in a last name (e.g. ‘Smith’) and have a  
query run against a table and return rows where a field (LastName) is  
‘Smith’.

Where I am hung up is the actual passing of the form value (let’s say  
the field name is ‘lastname’) to my controller  
(student\_search\_controller.rb) to the method which will execute the  
query.

In my form, I have:

\<%= start\_form\_tag :action =\> ‘searchresults’ %\>

Last Name  
\<%= text\_field 'student', 'searchfield' %\>

 \<%= submit\_tag "Search" %\> \<%= end\_form\_tag %\>

In my Controller I have:

def searchresults

```
lastname = params[:lastname]
@students = Student.find(:all,
                        :conditions => ["LastName = :lastname", 

```

params])  
end

However, I get the following error:

ActiveRecord::PreparedStatementInvalid in Student\_search#searchresults

wrong number of bind variables (4 for 1) in: LastName = :lastname

Can anyone provide a simple example? I can execute the query by  
hard-coding it in my searchresults method, but obviously…that’s not  
really what I need here.

Thanks!  
Grant

---

<div class="post-metadata">

**Author:** ![Grant\_GBrown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9de0a6/32.png) [@Grant\_GBrown](https://www.ruby-forum.com/u/Grant_GBrown)\
**Post date:** [December 20, 2005, 12:46am UTC](https://www.ruby-forum.com/t/help-on-simple-search/50684/2 "2005-12-20T00:46:18Z")

</div>

Grant GBrown wrote:

> Hey there,

P

> Can anyone provide a simple example? I can execute the query by  
> hard-coding it in my searchresults method, but obviously…that’s not  
> really what I need here.

Your data should actually be under params[:student][:searchfield],  
as that is what you named the textfield (see the API docs). You  
can also inspect the structure for more information:

$stderr.puts "Params: #{params.inspect} # Shows in webrick output

Now, generally, all Rails questions should go to:

[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Post there if you have any follow-ups! 🙂

> Thanks!  
> Grant

E
