搜索 Rails 引擎:无结果

Ransack search on Rails Engine: No results

正在尝试创建一个站点范围的搜索表单来搜索包含某些字符串的产品。

我使用的是稍作修改的 o Shoppe Gem 版本。它是一个用于管理购物车、产品、订单等的引擎...

我已将其上传到此存储库中:egallart´s shoppe fork

我已经更新了 shoppe 依赖项以匹配当前的 ransack 主版本。


我在应用程序布局中对我的搜索表单进行了编码,在 header 部分中,用于在数据库的名称列中进行搜索。 当我尝试一些我知道存在的字符串时,我没有在视图中得到任何结果。

这是我的 MVC 的相关代码。

应用程序控制器

在主应用中:app/controllers/application_controller.html.erb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  before_filter :set_search

  def set_search
    @q = Shoppe::Product.ransack(params[:q])
    @products = @q.result(distinct: true)
  end

end

我需要这个来初始化@q 站点范围,因为我得到了部分表单,该表单显示在站点每个页面的 header 中。

我不确定这是执行此操作的正确方法。

产品负责人

位于主应用中。

class ProductsController < ApplicationController

  def search
    @q = Shoppe::Product.ransack(params[:q])
    @products = @q.result(distinct: true)
  end

end

产品型号

位于 Shoppe::Engine

我没有触及与搜查相关的任何内容,引擎中没有特定的搜查设置。

搜索局部视图

在主应用程序中: views/layouts/application.html.erb

<%= render 'layouts/menuhorizontal' %>

和里面 views/layouts/_menuhorizontal.html.erb

<%= render 'products/search' %>

并在 views/products/_search.html.erb

里面
<%= form_for @q, url: search_products_path, method: :get, html: { id: "searchbox"} do |f| %>
<%= f.search_field :name_cont, html: { class: "search_query form-control", id: "search_query_top" } %>
<%= f.button "#{'<span>Buscar</span>'}".html_safe, class: "btn btn-default button-search", name: "submit_search" %>
<% end %>

搜索结果视图

views/products/search.html.erb

<% @products.each do |p| %>
<%= p.name %>
<% end %>

Routes.rb 在主应用中。

在 config/routes.rb 我已经设置:

resources :products do
  collection do
    get 'search'
  end
end

Routes.rb 在 Shoppe::Engine.

在 config/routes.rb 我没有碰任何东西。这是与 :products:

相关的 Shoppe::Engine vanilla 版本中显示的内容
resources :products do
  resources :variants
  resources :localisations, controller: "product_localisations"
  collection do
    get :import
    post :import
  end
  member do
    get :clone
  end
end

最重要的是:

当我尝试在 rails 控制台中搜索时,我也没有得到任何结果。这种行为让人感觉很奇怪,让我迷路了。

这是我在控制台中得到的:

[2] pry(main)> Shoppe::Product.ransackable_attributes
=> ["id",
"parent_id",
"name",
"sku",
"permalink",
"description",
"short_description",
"active",
"weight",
"price",
"cost_price",
"tax_rate_id",
"created_at",
"updated_at",
"featured",
"in_the_box",
"stock_control",
"default",
"ean",
"price_on_web",
"length",
"width",
"height",
"moq"]

[4] pry(main)> Shoppe::Product.ransack(name_cont: 'Acople')
=> Ransack::Search<class: Shoppe::Product, base: Grouping
<conditions: [Condition <attributes: ["name"], predicate: cont, 
values:     ["Acople"]>], combinator: and>>

注意:'Acople' 一个产品的数据库列名称中存在字符串。

另一个奇怪的行为

当我通过 sku 搜索时:(sku_cont:) 我得到了正确的结果。

name: 是一个字符串 sku: 也是一个字符串!!

好的,问题是 Shoppe 使用全球化进行翻译,并且数据库中的某些列不包含任何信息,因为它使用其他 table 来存储该信息。

现在我正在寻找一种在该翻译中进行搜索的方法。

更新

关注此link了解更多信息