Passing instance through AJAX

Hi,

I need to pass variable in the search form to an AJAX partial but it
doesn’t work. please tell me how to pass @groceries to the partial.

{{FORM}}

<% form_remote_tag :url => {:action => :search}, :method => “get” do
%>

<%= text_field_tag(“query”, params[‘query’], :size => 20 ) %>
<%= submit_tag “Search” %>

{{CONTROLLER}}

def search

groceries_per_page = 4

  @groceries = Groceries.search params[:query], {:page =>

params[:page], :per_page => groceries_per_page}

respond_to do |format|
format.js
end

if request.xml_http_request?
render :partial => “search”, :layout => false
end
end

{{PARTIAL}}

<% if @groceries == nil %>
@groceries is nil
<% else %>
<% for groceries in @groceries %>
groceries.name
<% end %>

{{search.js.rjs}}

page.replace_html(“sgreplace”, :partial => “search” )

<% if @groceries == nil %>
@groceries is nil
<% else %>
<% for groceries in @groceries %>
<%= groceries.name %>
<% end %>

should work

oops

should be
<%= @groceries.name %>

of course

I need to pass variable in the search form to an AJAX partial but it
doesn’t work. please tell me how to pass @groceries to the partial.

…was the question :slight_smile:

ok, then your query fails.

@groceries = Groceries.search params[:query], {:page =>
params[:page], :per_page => groceries_per_page}

return nil. that’s nothing to do with the partial.
The query looks a bit strange.

  1. It should be Grocery not Groceries (and this should give you an
    error)
  2. It should be find, not search. Are you using any plugin like ferret
    for the search?
    Then you should mention that.

Does params[:query] have a value? Does it search anything at all?
My guess: @groceries is nil all the time, because the query fails…

The partial is not my problem because I get the return on the ajax as
@groceries is nil which means it cannot pass through the first if
statement.
Which also means that @groceries is not being passed to the partial
through
AJAX

Its not the query because without ajax it works. The groceries is
correct
and the reason why it is search is because I am using thinking sphinx
not
find for my searching in the app.

When I do <%= params[:query] %> in the partial it shows my query which
is
red. This is really weird everthing but the instance is passing through.
Thinking sphinx anomally?