使用rails ajax search

时间:2016-07-05 10:37:41

标签: javascript jquery ruby-on-rails ajax

我想在我的rails应用程序中实现ajax搜索。那么我该如何处理我定义的代码呢。

index.html.erb

  <% @balance = 0 %>


     <div class="row">

    <div class="col-md-10 col-md-offset-1">

        <div class="table-responsive myTable">

            <table class="table listing text-center">
                <tr class="tr-head">
                    <td>Date</td>
                    <td>Description</td>
                    <td>Amount</td>
                    <td>Discount</td>
                    <td>Paid</td>
                    <td>Balance</td>
                </tr>

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


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

                <div id="sample">

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

                        <center><p><em>No results found.</em></p></center>              

                        <% end %>

                        <%= render @xvaziris %>

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

_xvaziri.html.erb

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

    <td class="col-1"><%= xvaziri.date.strftime('%d/%m/%Y') %></td>
    <td class="col-3"><%= span_with_possibly_red_color xvaziri.description %></td>


    <td class="col-1"><%= number_with_precision(xvaziri.amount, :delimiter => ",", :precision => 2) %></td>

    <td class="col-1 neg"><%= number_with_precision(xvaziri.discount, :delimiter => ",", :precision => 2) %></td>

    <td class="col-1 neg"><%= number_with_precision(xvaziri.paid, :delimiter => ",", :precision => 2) %></td>


    <% @balance += xvaziri.amount.to_f - xvaziri.discount.to_f - xvaziri.paid.to_f %>

    <% color = @balance >= 0 ? "pos" : "neg" %>

    <td class="col-1 <%= color %>"><%= number_with_precision(@balance.abs, :delimiter => ",", :precision => 2) %></td>

</tr>

xvaziris_controller.rb

class XvazirisController < ApplicationController

    before_action :set_xvaziri, only: [:show, :edit, :update, :destroy]


    def index
        @xvaziris = Xvaziri.where (["description LIKE ? OR amount LIKE ? OR paid LIKE ?", "%#{params[:search]}%","%#{params[:search]}%","%#{params[:search]}%"]) 
        respond_to do |format|
            format.js
            format.html
        end 
    end

    def import
        Xvaziri.import(params[:file])
        redirect_to xvaziris_url, notice: "Xvaziris imported."
    end

    def show
    end

    def new
        @xvaziri = Xvaziri.new
    end

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

    def edit
    end

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

    end

    def destroy
        @xvaziri.destroy
        flash[:notice] = 'Xvaziri was successfully destroyed.'
        redirect_to xvaziris_url    
    end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def xvaziri
        params.require(:xvaziri).permit(:date, :description, :amount, :discount, :paid)
    end

end

我想搜索&#39; / xvaziris&#39;的所有搜索结果。这是来自@xvaziris而没有重新加载页面。

index.js.erb的

 <% @xvaziris.each do |xvaziri| %>
  $('#kola').append("<%= j render xvaziri %>");
<% end %>

当我运行上述代码时,它会在我的终端中收到错误,如下所示;

Screenshot-1.png

我的索引页面样式和功能变得更糟,如下所示;

Screenshot..png

如何编写上述代码以便在xvaziris #index?

中查找搜索

1 个答案:

答案 0 :(得分:0)

试试这个:

1.在<property> <name>hive.downloaded.resources.dir</name> <value>/home/hduser/hive/tmp/${hive.session.id}_resources</value> <description>Temporary local directory for added resources in the remote file system.</description> 文件夹下创建一个文件(我假设?):xvaziris。您需要此文件来呈现单个项目,例如:

_xvaziri.html.erb

注意:我在<li> #Render individual xvaziris here </li>

中打包内容

2.在<li>中,添加:

index.js.erb

这就是你想要的吗?