AJAX Search w/database results


#1

Hey,

I have a main page that lists a table of Devices, along with a Search
box. I’d like to have the Search field pull up any related Devices,
and then re-populate the table.

I can do this without AJAX, just calling my List action over again,
passing the params to search on, but with AJAX I get the error,
“Cannot convert nil to string” on this line:

@device_pages, @devices = paginate( :devices,
:conditions => [“description like
?”,’%’+params[:description]+’%’],
:per_page => 10)

It is falling apart on the ‘+’ portion. Can anybody help with the
process of this? All the AJAX examples I’ve seen only render one line
of text, not a whole group of dynamic text.


My List html:

<%= form_remote_tag(:update => “results”,
:url => { :action => :search },
:position => “bottom” ) %>
<%= text_field_with_auto_complete :device, :description %>

Description

<%= submit_tag “Search” %>
<%= end_form_tag %>

<% if params[:description] %>

Results for '<%=h params[:description] %>'

<% end %>

My ‘search’ rhtml:

<% for device in @devices %>

<tr>

<td><%=h device.make %></td>
<td><%=h device.model %></td>
<td><%=h device.description %></td>
<td><%= device.person.fname %> <%= device.person.lname %></td>


<% end %>

My DevicesController for the two actions:
def list
device_pages, @devices = paginate :devices, :per_page => 10

end
def search

@device_pages, @devices = paginate( :devices,
  	               :conditions => ["description like

?",’%’+params[:description]+’%’],
:per_page => 10)

end

  • Nic

#2

Does your params hash have a key of :description? Check what the params
are in log/development.log.

My guess is you want params[:device][:description] in your search
action.

-Jonny

Nic W. wrote:

Hey,

I have a main page that lists a table of Devices, along with a Search
box. I’d like to have the Search field pull up any related Devices,
and then re-populate the table.

I can do this without AJAX, just calling my List action over again,
passing the params to search on, but with AJAX I get the error,
“Cannot convert nil to string” on this line:

@device_pages, @devices = paginate( :devices,
:conditions => [“description like
?”,’%’+params[:description]+’%’],
:per_page => 10)

It is falling apart on the ‘+’ portion. Can anybody help with the
process of this? All the AJAX examples I’ve seen only render one line
of text, not a whole group of dynamic text.


#3

Thanks Jonathan, I figured out later that my problem is accessing the
params correctly. I’m still having trouble though:

I tried this: if params[:device][:description]

and I get ‘Symbol as Array’

If I put parentheses: if params([:device][:description])

You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.[]

The API wasn’t too helpful on accessing nested params, any ideas?

  • Nic.

On 1/25/06, Jonathan V. removed_email_address@domain.invalid wrote:

?",’%’+params[:description]+’%’],
Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

  • Nic

#4

Can you post the raw params hash straight from the log file?

-Jonny.

Nic W. wrote:

Thanks Jonathan, I figured out later that my problem is accessing the
params correctly. I’m still having trouble though:

I tried this: if params[:device][:description]

and I get ‘Symbol as Array’

If I put parentheses: if params([:device][:description])

You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.[]

The API wasn’t too helpful on accessing nested params, any ideas?

  • Nic.

#5

Processing DevicesController#search (for 127.0.0.1 at 2006-01-25
11:31:04) [POST]
Parameters: {“device”=>{“description”=>“color”}, “commit”=>“Search”,
“action”=>“search”, “controller”=>“devices”}

On 1/25/06, Jonathan V. removed_email_address@domain.invalid wrote:

and I get ‘Symbol as Array’


Posted via http://www.ruby-forum.com/.


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

  • Nic

#6

It ended up working as @params[:device][:description], thanks for your
help.

On 1/26/06, Jonathan V. removed_email_address@domain.invalid wrote:

“action”=>“search”, “controller”=>“devices”}
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

  • Nic

#7

Are you using Rails 1.0?

Nic W. wrote:

It ended up working as @params[:device][:description], thanks for your
help.

  • Nic

#8

You should be able to access that description value of ‘color’ with:

params[‘device’][‘description’]

-Jonny.

Nic W. wrote:

Processing DevicesController#search (for 127.0.0.1 at 2006-01-25
11:31:04) [POST]
Parameters: {“device”=>{“description”=>“color”}, “commit”=>“Search”,
“action”=>“search”, “controller”=>“devices”}

On 1/25/06, Jonathan V. removed_email_address@domain.invalid wrote:

and I get ‘Symbol as Array’


#9

Yep.

gem list:

rails (1.0.0)

On 1/26/06, Jonathan V. removed_email_address@domain.invalid wrote:


Posted via http://www.ruby-forum.com/.


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

  • Nic

#10

Strange, you shouldn’t have to use @params, params by itself should work
fine. Oh well, just one of those strange things that makes life
interesting :wink:

-Jonny

Nic W. wrote:

Yep.

gem list:

rails (1.0.0)