使用Ajax Rails返回搜索结果时获取重复记录 [英] Getting Duplicate records when returning search results with Ajax Rails

查看:73
本文介绍了使用Ajax Rails返回搜索结果时获取重复记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个发票的应用程序,用户可以搜索特定的发票.我正在使用Ajax,以便用户可以进行搜索查询.

I have an app that has several invoices and users can search for specific invoices. I'm using Ajax so that users can make a search query.

问题是,当用户搜索发票号"7289"时,它找到了记录,但是记录重复.

The issue is when a user searches for invoice no."7289" it found the records but duplicate records.

以下是屏幕截图;

下面是代码;

index.js.erb

index.js.erb

$('#kola tbody').empty();

<% if @shippingdetails.empty? %>
$('#kola tr').remove();
$('#kola tbody').append("No Results Found For Your Search...");
$("#kola tbody").css({"background-color": "white", "font-size": "100%", "font-weight": "900"}); 
<% else %>
<% @shippingdetails.each do |shippingdetail| %>
$('#kola tbody').append("<%= j render shippingdetail %>");
<% end %>
<% end %>

index.html.erb

index.html.erb

<div class="row">
    <div class="col-md-5 col-md-offset-3">

        <div class="table-responsive myTable">

            <table id = "kola" class="table listing text-center">
                <thead>
                    <tr class="tr-head">
                        <td>Invoices</td>
                    </tr>
                </thead>


                <a href="#" class="toggle-formed" style="float: right;" ><strong>Search</strong></a>

                <div id="sample">

                    <%= form_tag shippingdetails_path, remote: true, method: :get, class: "form-group", role: "search" do %>
                    <p>
                        <center><%= text_field_tag :search, params[:search], placeholder: "Search for Invoices.....", autofocus: true, class: "form-control-search" %>
                            <%= submit_tag "Search", name: nil, class: "btn btn-md btn-primary" %></center>
                        </p>
                        <% end %><br>
                    </div>


                    <tr>
                        <td></td>
                    </tr>

                <tbody>              
                    <%= render @shippingdetails %>
                </tbody>

            </table>
        </div>
    </div>
</div>

_shippingdetail.html.erb

_shippingdetail.html.erb

<tr class="tr-<%= cycle('odd', 'even') %>">

    <td class="col-1"><%= link_to shippingdetail, shippingdetail %></td>

</tr>

shippingdetails_controller.rb

shippingdetails_controller.rb

class ShippingdetailsController < ApplicationController
  before_action :set_shippingdetail, only: [:show, :edit, :update, :destroy]


  def index
    @shippingdetails = Shippingdetail.search(params[:search])

    respond_to do |format|
      format.js
      format.html 
    end  
  end

  def show
  end

  def new
    @shippingdetail = Shippingdetail.new
  end

  def create
    @shippingdetail = Shippingdetail.new(shippingdetail_params)
    if
      @shippingdetail.save
      flash[:notice] = 'Shippingdetail Created'
      redirect_to @shippingdetail
    else
      render 'new'
    end
  end

  def edit
  end

  def update
    if @shippingdetail.update(shippingdetail_params)
     flash[:notice] = 'Shippingdetail Updated'
     redirect_to @shippingdetail
   else
    render 'edit'
  end

end

def destroy
  @shippingdetail.destroy
  flash[:notice] = 'Shippingdetail was successfully destroyed.'
  redirect_to shippingdetails_url   
end

private
    # Use callbacks to share common setup or constraints between actions.
    def set_shippingdetail
      @shippingdetail = Shippingdetail.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def shippingdetail_params
      params.require(:shippingdetail).permit(:invnos, :shippeddate, :cusname, :lanchno, :capname, :contactno, :markup, :brand, :season, :quantity, :cartons, :goonis, :image)
    end

  end

如何防止Rails Ajax复制记录?

How can I prevent rails ajax from duplicating records?

任何建议都是最欢迎的.

Any suggestions are most welcome.

谢谢.

推荐答案

我刚刚在代码中替换了id ="kola",如下所示;

I just replaced the id="kola" in my code to look like as below;

index.html.erb

index.html.erb

<div class="row">
    <div class="col-md-5 col-md-offset-3">

        <div class="table-responsive myTable">

            <table class="table listing text-center">
                <thead>
                    <tr class="tr-head">
                        <td>Invoices</td>
                    </tr>
                </thead>

                    <tr>
                        <td></td>
                    </tr>

                <tbody id = "kola">              
                    <%= render @shippingdetails %>
                </tbody>

            </table>
        </div>
    </div>
</div>

index.js.erb

index.js.erb

我只是省略了"tbody",而将"tr"替换为"tr-head",如下所示;

I just omitted 'tbody' and replace 'tr' with 'tr-head' as below;

    $('#kola').empty();
    <% if @shippingdetails.empty? %>
    $('.tr-head').remove();
    $('#kola').append("No Results Found For Your Search...");
    $('#kola').css({"background-color": "white", "font-size": "100%", "font-weight": "900"}); 
    <% else %>
    <% @shippingdetails.each do |shippingdetail| %>
    $('#kola').append("<%= j render shippingdetail %>");
    <% end %>
    <% end %>

现在一切正常.

这篇关于使用Ajax Rails返回搜索结果时获取重复记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆