0

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

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

我已经把它上传到这个存储库:egallart´s shoppe fork

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


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

这是我的 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 站点范围,因为我已经获得了部分表单,该表单显示在站点每个页面的标题中。

我不确定这是正确的方法。

 产品控制器

位于主应用程序中。

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/_menuhorizo​​ntal.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 %>

搜索结果视图

意见/产品/search.html.erb

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

主应用程序中的 Routes.rb。

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

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

Shoppe::Engine 中的 Routes.rb。

在 config/routes.rb 我什么都没碰。这是 Shoppe::Engine vanilla 版本中显示的与 :products 相关的内容:

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: 也是一个字符串!!

4

1 回答 1

0

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

现在我正在寻找一种方法来搜索该翻译。

更新

点击此链接了解更多信息

Ransack 搜索和翻译

于 2015-11-30T12:02:46.070 回答