[英]Search with ransack gem
我在視圖中顯示查詢的結果,該查詢使用兩個不同的表,並且對於數據搜索,我使用了ransack gem執行搜索,但在搜索表單中顯示了以下錯誤:
ActionView::Template::Error (undefined method `Producto_cont' for Ransack::Search<class: Stoc, base: Grouping <combinator: and>>:Ransack::Search):
我從另一個稱為“ productos”的表中獲取“ Producto”字段,它是查詢的結果,它只向我顯示與我在查詢中相關的另一個字段的錯誤,而不是與我相關的錯誤我從“ Stoc”方法中調用
這是我的方法控制器:
@search = Stoc.productos_de_un_stock(params).search(search_params)
@search.sorts = 'Stock desc' if @search.sorts.empty?
@stock = @search.result().page(params[:stock]).per(15)
這是我的查詢:
def self.productos_de_un_stock(params)
query = select("[stock].IdStock, [stock].Stock, [productos].Clave AS Clave_producto, [productos].Producto, [productosxpzas].PzaXCja AS PzaXCja")
.joins('inner join productos ON stock.Articulo = productos.Clave inner join productosxpzas ON productos.Clave = productosxpzas.Producto')
.where('stock.Ruta = ? AND stock.IdEmpresa = ?', (params[:search]), (params[:search6]))
query
end
這是我的搜索表:
<%= search_form_for @search, :remote=>"true", url: busqueda_stock_path, :method => :get do |f| %>
<div class="rwd">
<table class="table table-striped table-bordered table-condensed table-hover rwd_auto">
<thead>
<tr>
<th class="component_name_header_col"><%= sort_link @search, :Articulo, "Clave","stoc", {}, { :remote => true, :method => :get } %></th>
<th class="action_col"><%= t("basic.action") %></th>
</tr>
<tr>
<th><%= f.text_field :Articulo_cont, class:"campo" %></th>
<%= f.search_field :Producto_cont %>
<input id="search" type="hidden" value="<%=params[:search] %>" name="search"/>
<input id="search6" type="hidden" value="<%=params[:search6] %>" name="search6"/>
<th><%= f.submit "Buscar" %></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<% end %>
首先,按照慣例,嘗試命名不帶大寫字母的列。
並且我們需要表和模型的結構來提供更好的答案,因此請嘗試將其添加到問題中。
但我會嘗試給您一些案例,以便您解決問題
如果您嘗試使用has_many關系,則將首先以復數形式調用該關系,然后再調用該模型的列,例如,如果用戶有很多項目,而我們要按項目名稱進行搜索,則就像這個
<%= f.search_field :projects_name_cont %>
如果用戶屬於一個機構,我們將使用單數形式,然后是模型的列,如下所示
<%= f.search_field :institution_name_cont %>
如果我們嘗試使用同一用戶模型中的列,那么它將直接
<%= f.search_field :name_cont %>
它將直接轉到用戶模型中的名稱列。 希望這可以幫助您了解
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.