Javascript Rails:使用搜索框中的结果数据填充表

Javascript Rails:使用搜索框中的结果数据填充表,javascript,jquery,ruby-on-rails,ruby,ajax,Javascript,Jquery,Ruby On Rails,Ruby,Ajax,正如问题所说,我试图用rails 4中搜索框的结果填充表中的数据。我用谷歌搜索它,但我找不到答案,甚至找不到一个关于如何做到这一点的例子 我的看法是: <% title @sale.client_name %> <div class="hero-unit"> <CENTER><H1><%= @sale.client_name %> </H1> <br> <p>

正如问题所说,我试图用rails 4中搜索框的结果填充表中的数据。我用谷歌搜索它,但我找不到答案,甚至找不到一个关于如何做到这一点的例子

我的看法是:

<% title @sale.client_name %>
<div class="hero-unit">
    <CENTER><H1><%= @sale.client_name %> </H1>
    <br>
    <p>
        Numero de Factura:<br>
        <%= @sale.check_number %>
    </p>
    <p>
        Nit del Cliente:<br>
        <%= @sale.nit%>
    </p>
    <p>
        Fecha de venta:<br>
        <%= @sale.created_at %>
    </p>
    <p>    
        Total Venta:<br>
        <%= @sale.price %>
   </p>
   <p>
       <h2><Center><%= link_to_function "Ver Detalle de Venta", "$('#hide').toggle()" %>          </Center></h2>
<div id="hide" style="display:none">
    <table class="table table-striped">
        <thead>
            <tr>
                <th><Center>Nombre</Center></th>
                <th><Center>Codigo del SubProducto</Center></th>
                <th><Center>Marca</Center></th>
                <th><Center>Categoria</Center></th>
                <th><Center>Precio</Center></th>        
            </tr>
        </thead>
        <tbody>
            <% @sale.subproducts.each do |subproduct| %>
                <tr>
                    <td><CENTER><%= subproduct.product.name %></CENTER></td>
                    <td><CENTER><%= subproduct.code %></CENTER></td>
                    <td><CENTER><%= subproduct.product.brand %></CENTER></td>
                    <td><CENTER><%= subproduct.product.category %></CENTER></td>
                    <td><CENTER><%= subproduct.product.sale_price %></CENTER></td>
                    <td><%= link_to 'Eliminar de venta', :controller => :subproducts,  action => 'eliminar_subproducto_venta', :id => subproduct.id, :sale_id => @sale.id %></td>
                </tr>
            <% end %>
        </tbody>
    </table>
</div>
</p>

<% if @sale.confirmed == false %>
    <p>
        <h2><Center><%= link_to_function "Agregar Producto a venta",   "$('#subproduct').toggle()" %></Center></h2>
    <div id="subproduct" style="display:none">
        <table class="table table-striped">
            <thead>
                <tr>
                      <th><Center>Nombre</Center></th>
                      <th><Center>Codigo del SubProducto</Center></th>
                      <th><Center>Marca</Center></th>
                  <th><Center>Categoria</Center></th>
                  <th><Center>Precio De Venta</Center></th>        
            </tr>
        </thead>
        <tbody>
            <% @subproducts.each do |subproduct| %>
                <tr>
                    <%if subproduct.available == true || subproduct.available == "NULL"%>
                    <td><CENTER><%= subproduct.product.name %></CENTER></td>
                    <td><CENTER><%= subproduct.code %></CENTER></td>
                    <td><CENTER><%= subproduct.product.brand %></CENTER></td>
                    <td><CENTER><%= subproduct.product.category %></CENTER></td>
                    <td><CENTER><%= subproduct.product.sale_price %></CENTER></td>

  <td><%= link_to 'Agregar a Venta', :controller => :subproducts, :action =>    'agregar_subproducto_venta', :id => subproduct.id, :sale_id => @sale.id %></td>
                        <% end %>
                    </tr>
               <% end %>
        </tbody>
    </table>
</div>
</p>
<%if @sale.price !=0 %>
<%= link_to 'Confirmar Venta', :controller => :sales, :action => 'confirm_sale',  :identificator => @sale.id%><br>
<%end%>

<%= link_to 'Cancelar Venta', :controller => :sales, :action => 'cancel_sale', :deletor => @sale.id%><br>
<%end%><br> 
<%= button_to 'Volver', sales_path, :method => :get, :class => 'btn btn-default' %><br>

</CENTER>
</div>
我希望当用户单击带有特定参数的搜索按钮时,可以刷新第二个表


有人能帮我吗??我已经尝试了将近两周,但没有结果=

有很多方法可以做到这一点。最简单的方法是为表创建js视图search_table.js.erb,它将返回呈现的html表

您的js视图:

$("#your_second_table_container").html(<%= escape_javascript(render(partial: 'search_results', locals: {subproducts: @subproducts}))%>)
以及您的搜索链接:

<%= link_to 'Search', <ypur_rout_to_js_view_action>, remote: true %>

你有一些例子吗??如果我不能完成明天的作业,我会有很多麻烦@ArayDo你有什么行动来搜索吗?正如我已经说过的,您应该使用.js.erb扩展为这个操作添加新视图。在这里,您应该使用jQuery从partial呈现表。然后您应该通过ajax.get请求这个js视图,或者简单地使用remote:true选项放置Rails链接。您还可以使用form_for与相同的remote:true属性,f.submitI将查看并注释我的结果。非常感谢你