Nature of Return Values from Search

Hello,
I’m trying the search method used in the Guides:

and I don’t know what is coming back to the controller when a text box
is empty. When each of the 2 text boxes (described below) have a
value then the search works. However, if the second box is empty then
nothing is returned, even though there is data to match the first box.
The boxes are formed in the search.html.erb as:

<%= form_tag({controller => “people”, :action => “search”}, :method =>
“get” ) do %>
<%= label_tag(:skill1, “Search Skills for:”) %>
<%= text_field_tag(:skill1) %>
<%= text_field_tag(:skill2) %>
<%= submit_tag(“Search”) %>
<% end %>

And are read in the controller by:

def search
@people = Person.all
@skill_search1 = String.new
@skill_search1=:skill1.to_s
@skill_search2 = String.new
@skill_search2=:skill2.to_s

if @skill_search2.empty?
  @found_people = Person.where("skill_set LIKE ?",

params[@skill_search1])
else
@found_people = Person.where(“skill_set LIKE ? and skill_set
LIKE ?”, params[@skill_search1],params[@skill_search2])
end
end

 I have also tried: if @skill_search2  == NUL, ==  " ",== nil,

==‘’ and =“” under the theory that if you type in everything then
something might work (hey, it’s worked in the past!).

 Looking at the output in the command window that is used for the

“rails server” call to WEBRick it seems that the second conditional
option is always called and the statement ends in “and skill_set LIKE
‘’” (that is 2 apostrophes before the final quote and, while it’s a
little hard to judge, I don’t think there is a space between the
apostrophes).
So the question is: what is being returned by the blank text box
and how should it be checked?
Thanks,
Barney

Hej

You probably should check the params hash in the controller:

@skill_search1 = params[:skill1] etc…

Also use a debugger call in the search method to see the
parameters that are passed

def search
debugger
.
.
end

Cheers,
Eric

Hi Eric,
I’m using Scite and there doesn’t seem to be a debugger in it.
How else would I check that hash?
But, could you tell me what form (type, value) is the return from
that empty text box?
Thanks,
Barney

Hej Barney

The debugger call will stop the execution and drop you into the debugger

  • it’s not from within your text editor - it’s in the
    terminal window where you run your app

Start your app with

rails server --debugger

or (if your running rails < 3.0 )

script/server --debugger

Cheers,
Eric

On Thu, Jul 28, 2011 at 3:59 PM, Barney [email protected] wrote:

How else would I check that hash?

Besides the previously mentioned debugger, you can add logging
statements to your code to provide more information.

But, could you tell me what form (type, value) is the return from
that empty text box?

It’s the web – all request parameters are strings; if no value was set
in the client then it’s an empty string.

But based on the code above –

@skill_search2 = String.new # blech - extraneous
@skill_search2=:skill2.to_s # ditto, also meaningless :slight_smile:

if @skill_search2.empty?

what you want is just

if params[‘skill2’].empty?

HTH,

Hassan S. ------------------------ [email protected]

twitter: @hassan

Thanks Eric, I didn’t know about the debugger.
When I ran: rails server --debugger I got the error that I needed to
install ruby-debug with ‘gem install ruby-debug’ but when I did the
error: “Failed to build gem native extension.” and then there were a
bunch of errors involving “no member” in RArray and RString. I then
successfully installed ruby-debug-ide19-0.4.12, “but the rails server
–debugger” gave the same error as before. Maybe I need to ask this
in a separate question, but if you know the answer to the problem off-
hand I’d appreciate knowing it.
Thanks,
Barney

Thanks Hassan, the code changed you mentioned above worked! I
appreciate your taking the time to help me,
Barney

On Jul 28, 7:25pm, Hassan S. [email protected]