# AJAX Search w/database results

**URL:** <https://www.ruby-forum.com/t/ajax-search-w-database-results/53099>\
**Category:** Rails\
**Created:** [January 25, 2006, 6:52am UTC](https://www.ruby-forum.com/t/ajax-search-w-database-results/53099 "2006-01-25T06:52:07Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nic\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/35a633/32.png) [@Nic\_W](https://www.ruby-forum.com/u/Nic_W)\
**Post date:** [January 25, 2006, 6:52am UTC](https://www.ruby-forum.com/t/ajax-search-w-database-results/53099/1 "2006-01-25T06:52:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Nic\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/35a633/32.png) [@Nic\_W](https://www.ruby-forum.com/u/Nic_W)\
**Post date:** [January 25, 2006, 1:09pm UTC](https://www.ruby-forum.com/t/ajax-search-w-database-results/53099/2 "2006-01-25T13:09:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Nic\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/35a633/32.png) [@Nic\_W](https://www.ruby-forum.com/u/Nic_W)\
**Post date:** [January 25, 2006, 7:13pm UTC](https://www.ruby-forum.com/t/ajax-search-w-database-results/53099/3 "2006-01-25T19:13:26Z")

</div>

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](mailto:removed_email_address@domain.invalid) wrote:

> > ?",‘%’+params[:description]+‘%’],  
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

- Nic

---

<div class="post-metadata">

**Author:** ![Nic\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/35a633/32.png) [@Nic\_W](https://www.ruby-forum.com/u/Nic_W)\
**Post date:** [January 25, 2006, 8:27pm UTC](https://www.ruby-forum.com/t/ajax-search-w-database-results/53099/4 "2006-01-25T20:27:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Nic\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/35a633/32.png) [@Nic\_W](https://www.ruby-forum.com/u/Nic_W)\
**Post date:** [January 25, 2006, 8:35pm UTC](https://www.ruby-forum.com/t/ajax-search-w-database-results/53099/5 "2006-01-25T20:35:18Z")

</div>

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](mailto:removed_email_address@domain.invalid) wrote:

> > and I get ‘Symbol as Array’
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

- Nic

---

<div class="post-metadata">

**Author:** ![Nic\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/35a633/32.png) [@Nic\_W](https://www.ruby-forum.com/u/Nic_W)\
**Post date:** [January 26, 2006, 5:48pm UTC](https://www.ruby-forum.com/t/ajax-search-w-database-results/53099/6 "2006-01-26T17:48:44Z")

</div>

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

On 1/26/06, Jonathan V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > “action”=\>“search”, “controller”=\>“devices”}  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

- Nic

---

<div class="post-metadata">

**Author:** ![Nic\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/35a633/32.png) [@Nic\_W](https://www.ruby-forum.com/u/Nic_W)\
**Post date:** [January 26, 2006, 11:44pm UTC](https://www.ruby-forum.com/t/ajax-search-w-database-results/53099/7 "2006-01-26T23:44:04Z")

</div>

Are you using Rails 1.0?

Nic W. wrote:

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

> –
> 
> - Nic

---

<div class="post-metadata">

**Author:** ![Nic\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/35a633/32.png) [@Nic\_W](https://www.ruby-forum.com/u/Nic_W)\
**Post date:** [January 26, 2006, 2:00pm UTC](https://www.ruby-forum.com/t/ajax-search-w-database-results/53099/8 "2006-01-26T14:00:00Z")

</div>

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](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > and I get ‘Symbol as Array’

---

<div class="post-metadata">

**Author:** ![Nic\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/35a633/32.png) [@Nic\_W](https://www.ruby-forum.com/u/Nic_W)\
**Post date:** [January 27, 2006, 1:18am UTC](https://www.ruby-forum.com/t/ajax-search-w-database-results/53099/9 "2006-01-27T01:18:49Z")

</div>

Yep.

gem list:  
…  
rails (1.0.0)  
…

On 1/26/06, Jonathan V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

- Nic

---

<div class="post-metadata">

**Author:** ![Nic\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/35a633/32.png) [@Nic\_W](https://www.ruby-forum.com/u/Nic_W)\
**Post date:** [January 27, 2006, 1:50pm UTC](https://www.ruby-forum.com/t/ajax-search-w-database-results/53099/10 "2006-01-27T13:50:09Z")

</div>

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 😉

-Jonny

Nic W. wrote:

> Yep.
> 
> gem list:  
> …  
> rails (1.0.0)  
> …
