【问题标题】:Getting Duplicate records when returning search results with Ajax Rails使用 Ajax Rails 返回搜索结果时获取重复记录
【发布时间】:2016-08-28 13:47:11
【问题描述】:

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

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

截图如下;

代码如下;

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

<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

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

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

</tr>

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 重复记录?

欢迎提出任何建议。

提前谢谢你。

【问题讨论】:

  • 在 index.js.erb 中,@shippingdetails 是否包含两条相同的记录?而且,这种情况会一直发生吗?我的意思是,比如你打开这个页面,结果是空的,你进入一个搜索团队,收到两个相同的结果?
  • 感谢您的回复。我得到了所有的搜索结果两次。如果搜索不匹配,那么我也会得到两次,即“没有为您的搜索找到结果......”。
  • 所以你的意思是@shippingdetails 变量包含两个对象?填充此变量并检查其内容后,您能否在控制器中停止代码执行?
  • 再次感谢,在此之后我是否需要使用“byebug”,def index @shippingdetails = Shippingdetail.search(params[:search]) 然后 byebug

标签: jquery ruby-on-rails ajax


【解决方案1】:

我刚刚将代码中的 id="kola" 替换为如下所示;

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

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

    $('#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 %>

现在一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多