在Rails引擎上搜索Ransack:没有结果

时间:2015-11-29 10:53:58

标签: ruby-on-rails rails-engines ransack search-form

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

我使用的是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

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

搜索部分视图

在主应用程序中: 视图/布局/ application.html.erb

<%= render 'layouts/menuhorizontal' %>

内部视图/布局/ _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版本中与以下内容相关的内容:产品:

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

1 个答案:

答案 0 :(得分:0)

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

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

更新

请点击此链接获取更多信息

Ransack search and translations