Forum: Ruby on Rails AJAX Search w/database results

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.
B0e227ae419295321ca2aff4c27ad99d?d=identicon&s=25 Nic Werner (Guest)
on 2006-01-25 06:52
(Received via mailing list)
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  %><br/>
<p><label for="description">Description</label><br/>

 <%= submit_tag "Search" %>
 <%= end_form_tag %>

<% if params[:description] %>
<h1>Results for '<%=h params[:description] %>'</h1>
<% end %>
<table id="results">
</table>

---------------
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>
   </tr>
   <% 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
337cdd270761e0e6f4356de45b04d388?d=identicon&s=25 Jonathan Viney (jonny)
on 2006-01-25 13:09
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 Werner 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.
>
B0e227ae419295321ca2aff4c27ad99d?d=identicon&s=25 Nic Werner (Guest)
on 2006-01-25 19:13
(Received via mailing list)
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 Viney <jviney@spreydon.org.nz> wrote:
> >
> > ?",'%'+params[:description]+'%'],
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
- Nic
337cdd270761e0e6f4356de45b04d388?d=identicon&s=25 Jonathan Viney (jonny)
on 2006-01-25 20:27
Can you post the raw params hash straight from the log file?

-Jonny.

Nic Werner 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.
>
B0e227ae419295321ca2aff4c27ad99d?d=identicon&s=25 Nic Werner (Guest)
on 2006-01-25 20:35
(Received via mailing list)
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 Viney <jviney@spreydon.org.nz> wrote:
> > and I get 'Symbol as Array'
> >
>
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
- Nic
337cdd270761e0e6f4356de45b04d388?d=identicon&s=25 Jonathan Viney (jonny)
on 2006-01-26 14:00
You should be able to access that description value of 'color' with:

params['device']['description']

-Jonny.

Nic Werner 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 Viney <jviney@spreydon.org.nz> wrote:
>> > and I get 'Symbol as Array'
>> >
>>
B0e227ae419295321ca2aff4c27ad99d?d=identicon&s=25 Nic Werner (Guest)
on 2006-01-26 17:48
(Received via mailing list)
It ended up working as @params[:device][:description], thanks for your
help.

On 1/26/06, Jonathan Viney <jviney@spreydon.org.nz> wrote:
> > "action"=>"search", "controller"=>"devices"}
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
- Nic
337cdd270761e0e6f4356de45b04d388?d=identicon&s=25 Jonathan Viney (jonny)
on 2006-01-26 23:44
Are you using Rails 1.0?

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

>
>
> --
> - Nic
B0e227ae419295321ca2aff4c27ad99d?d=identicon&s=25 Nic Werner (Guest)
on 2006-01-27 01:18
(Received via mailing list)
Yep.

gem list:
....
rails (1.0.0)
...

On 1/26/06, Jonathan Viney <jviney@spreydon.org.nz> wrote:
>
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
- Nic
337cdd270761e0e6f4356de45b04d388?d=identicon&s=25 Jonathan Viney (jonny)
on 2006-01-27 13:50
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 Werner wrote:
> Yep.
>
> gem list:
> ....
> rails (1.0.0)
> ...
>
This topic is locked and can not be replied to.