Topic: Replace HTML by partial using jQuery

I can't figure out what is the problem, I suppose it is the syntax or file extension. What I am trying to do is very simple: I use a text field with autocomplete (that works) and a button 'search' to find Enterprises. So that's how I defined it:

#routes.rb

BillingService::Application.routes.draw do
resources :enterprises do
    get :autocomplete_enterprise_name, :on => :collection
    get 'search', :on => :collection
  end  
  #map.search "search", :controller => "enterprises"
  root :to => "enterprises#index"
end

#EnterprisesController

class EnterprisesController < ApplicationController
  autocomplete :enterprise, :name

  def index
    @enterprises = Enterprise.paginate :page => params[:page], :order => 'name', :per_page => 20
  end

  def search    
    respond_to do |format|
      if params[:name].blank?
        error = "The search field can not be empty!"
        format.js do
          render(:update) { |page| page.call :alert, error }
        end
      else
        @entreprises = []
        results = Enterprise.find_all_by_name(params[:name])    
        @enterprises = results.paginate(:page => params[:page], :order=> :name, :per_page=> 10) if results
        format.js
      end
    end
  end
...
end

#views/enterprises/index.html.erb:

<% title "Enterprises" %>

<%if @enterprises.empty?%>
  <h2>No enterprises found</h2>
<%else%>
  <h1>Welcome!</h1>
  <%= form_tag(search_enterprises_path, :method  => "get") do %>
    <%=text_field_tag 'name', '', :autocomplete => autocomplete_enterprise_name_enterprises_path %>
    <%= submit_tag "Search" %>
  <% end %>
  <div id="list">
    <%= render(:partial => "enterprise" , :object => @enterprises) %>
  </div>
<%end%>
<p><%= link_to "New", new_enterprise_path %></p>

#partial views/enterprises/_enterprise.html.erb:

<%if @enterprises.empty?%>
  <h2>Nothing found></h2>
<%else%>
  <table>
    <tr>
      <th>Client Nr</th>
      <th>Name</th>
      <th>Denomination</th>
      <th>Form</th>
    </tr>
    <% for enterprise in @enterprises %>
      <tr class="<%= cycle('rowA', 'rowB') %>">
        <td><%=enterprise.client_nr %></td>
        <td><%=enterprise.name %></td>
        <td><%=enterprise.denomination %></td>
        <td><%=enterprise.form %></td>
        <td><%= link_to  "Services", enterprise %></td>
        <td><%= link_to "Edit", edit_enterprise_path(enterprise) %></td>
        <td><%= link_to "Destroy", enterprise, :confirm => "Arer you sure", :method => :delete %></td>
      </tr>
    <% end %>
  </table>
  <%= will_paginate @enterprises %>
<%end%>

And at list jQuery rjs page, views/enterprises/search.js.erb:

$('#list').replaceWith('<%= escape_javascript(render(:partial => "enterprise", :object=> @enterprise)) %>');

The problem is that when I click on 'Search' button, the query returns the results I need but I have nothing displayed. So I'm asking what is wrong that I did? I've already tried to replace the extension of 'js' template as 'search.js.erb' or search.js.rjs',  - still noting is rendered. sad
Thanks.

Last edited by Javix (2011-01-28 04:40:08)

Re: Replace HTML by partial using jQuery

OK, I figure out how to solve it. The simple ':remote=> true' missed in my form.So the below is an updated version of the code:
#index.html.erb

<% title "Enterprises" %>

<div id="enterprises_list"><%= render :partial => "enterprises" %></div>
<p><%= link_to "New", new_enterprise_path %></p>

partial views/enterprises/_enterprises.html.erb:

<%if @enterprises.empty?%>
  <h2>Nothing found></h2>
<%else%>
<%= form_tag(search_enterprises_path, :method  => "get", :remote=> true) do %>
    <%=text_field_tag 'name', '', :autocomplete => autocomplete_enterprise_name_enterprises_path %>
    <%= submit_tag "Search" %>
  <% end %>
  <table>
    <tr>
      <th>Client Nr</th>
      <th>Name</th>
      <th>Denomination</th>
      <th>Form</th>
    </tr>
    <% for enterprise in @enterprises %>
      <tr class="<%= cycle('rowA', 'rowB') %>">
        <td><%=enterprise.client_nr %></td>
        <td><%=enterprise.name %></td>
        <td><%=enterprise.denomination %></td>
        <td><%=enterprise.form %></td>
        <td><%= link_to  "Services", enterprise %></td>
        <td><%= link_to "Edit", edit_enterprise_path(enterprise) %></td>
        <td><%= link_to "Destroy", enterprise, :confirm => "Arer you sure", :method => :delete %></td>
      </tr>
    <% end %>
  </table>
  <%= will_paginate @enterprises %>
<%end%>

views/enterprises/search.js.erb:

$("#enterprises_list").html("<%= escape_javascript(render(:partial => "enterprises")) %>");

The code in the controller is the same. Hope this helps. smile