如何在rails中显示搜索结果?

时间:2017-03-22 12:34:15

标签: ruby-on-rails

当我搜索store_item时,我有两个模型缩进和store_item如何在缩进索引页面上显示搜索结果。这是我的代码:

缩进/ new_indent:

<div class="form-group">
 <%=render 'item'%>
</div>

<div class="col-md-3"></div>
<div class="col-md-7">
<h3>Store Item</h3>
<div class="form-group">
 <%= form_for :indent, url:{action:'search_item'}, method: :get,remote:true,html:{id: "user_form"} do |f|%>
 <tbody>
   <tr>
    <td class="col-3"><%= f.select :id, StoreItem.all.collect {|b| [b.item_name,b.id]},{prompt:"Select Item"},{onchange:"$('#user_form').submit();"}%></td>
   </tr>
 <%end%>
</tbody>
</div>
</div>
<div class="search_item"></div>

Search.js.erb:

$('.search_item').html("<%= j render 'store_item' %>");

store_item.rb:

  def self.search_item(a)
   StoreItem.where("id LIKE ?", "%#{a}%")
  end

_store_item.html.erb

<div class="col-md-3"></div>
<div class="col-md-7">
      <% @storeitems.each do |s| %>
        <tr>
          <th>Unit Price</th>:
          <td><%= s.unit_price %></td>
          <br>
          <th>Batch No</th>:
          <td><%= s.batch_no%>
          <br>
          <th>Available</th>:
          <td><%= s.quantity%>
        </tr>
      <% end %>   
</div>
</div>  

Indent_controller:

class IndentsController < ApplicationController
    def index_indent
     @stores = Store.all
     @indents = Indent.all
     @search = params[:indent_id]
     @storeitems = StoreItem.search_item(@search)
    end

def search_item
 @storeitems = StoreItem.search_item(params[:indent][:id])
end

def create_indent
 @indent = Indent.new(indent_params)
 if @indent.save
    redirect_to index_indent_indents_path
 else
  render 'index_indent'
 end
end

indent_index:

<table id="datatable" class="table table-striped table-bordered responsive">
   <thead>
    <tr>
      <th>Indent No</th>
      <th>Store</th>
      <th>Expected Date</th>
      <th>Description</th>
      <th>Required</th>
      <th>Pending</th>
      <th>Issued Quantity</th>
      <th>Issued Type</th>
      <th>Destroy Indent</th>
      <th>Unit Price</th>
      <th>Batch No</th>
      <th>Available</th>
      <th colspan="3"></th>
    </tr>
  </thead>

   <% @indents.each do |i| %>
    <tr>  
        <td><%= i.indent_no %></td>
        <td><%= @stores.name %></td>
        <td><%= i.expected_date%></td>
        <td><%= i.description %></td>
        <td><%= i.required %></td>
        <td><%= i.pending %></td>
        <td><%= i.issued_qty %></td>
        <td><%= i.issued_type %></td>
        <td><%= link_to 'Destroy', destroy_indent_indent_path(i), method: :delete, data: { confirm: 'Are you sure?' },class: "btn btn-danger btn-sm" %></td>

        <%= render partial: 'store_item' %>
      </tr>
 <%end%>

0 个答案:

没有答案