Forum: Ruby Help on Simple Search

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Adc6ac29a4494e3c7a0fa84be23e7aea?d=identicon&s=25 Grant GBrown (Guest)
on 2005-12-19 23:25
(Received via mailing list)
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' %>
<p><label for="Lastname">Last Name</label><br/>
<%= text_field 'student', 'searchfield'  %></p>
  <%= 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
Fe9b2d0628c0943af374b2fe5b320a82?d=identicon&s=25 Eero Saynatkari (rue)
on 2005-12-20 00:46
Grant GBrown wrote:
> Hey there,
>
P <snip/>
>
> 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

Post there if you have any follow-ups! :)

> Thanks!
> Grant


E
This topic is locked and can not be replied to.