在检查时通过多个 select(复选框)进行搜查
Ransack search by multiple select(checkbox) on check
在我的代码中没有显示错误,我正在使用 Ransak 搜索,但我没有得到搜索输出。当我从复选框中检查大小选项时,我想获得输出。如果我点击中号,我想显示所有中号连衣裙,喜欢所有
请给我一个解决方案
这是我的复选框
这是我的代码
product/index.html.slim
= search_form_for @product_search, url: shop_index_path do |f|
= f.label :size_cont, "Size Available"
- StandardSize.all.each do |s|
= check_box_tag('product_search[standard_sizes_id_eq_any_cont][]', s.id )
= s.name
这是我的 ShopController.rb
class ShopController < ApplicationController
def index
@product_search = Product.ransack(params[:q])
@products = @product_search.result(distinct:true).page(params[:page]).per(8)
@product_search.build_sort if @product_search.sorts.empty?
end
型号standardsize.rb
class StandardSize < ActiveRecord::Base
belongs_to :product
end
这里是我的模型product.rb
class Product < ActiveRecord::Base
has_and_belongs_to_many :standard_sizes
end
这是我的服务器
Started GET "/shop?utf8=%E2%9C%93&q%5Bname_cont%5D=dress&q%5Bprice_paisas_gteq%5D=&q%5Bprice_paisas_lteq%5D=&product_search%5Bstandard_sizes_id_eq_all%5D%5B%5D=3&commit=Search" for 10.0.2.2 at 2015-07-23 10:12:42 +0000
Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by ShopController#index as HTML
Parameters: {"utf8"=>"✓", "q"=>{"name_cont"=>"dress", "price_paisas_gteq"=>"", "price_paisas_lteq"=>""}, "product_search"=>{"standard_sizes_id_eq_all"=>["3"]}, "commit"=>"Search"}
User Load (0.1ms) SELECT
用户.* FROM
用户WHERE
用户.
id= 1 ORDER BY
用户.
idASC LIMIT 1
除非您以其他方式配置 Ransack,否则所有搜索参数都应嵌套在 @q
下。我也用 _in
而不是 _eq_any_cont
.
取得了成功
替换
= search_form_for @product_search, url: shop_index_path do |f|
= f.label :size_cont, "Size Available"
- StandardSize.all.each do |s|
= check_box_tag('product_search[standard_sizes_id_eq_any_cont][]', s.id )
= s.name
和
= search_form_for @q, url: shop_index_path do |f|
= f.label :size_cont, "Size Available"
- StandardSize.all.each do |s|
= f.check_box :standard_sizes_id_in, {multiple: true}, s.id, nil
= s.name
在我的代码中没有显示错误,我正在使用 Ransak 搜索,但我没有得到搜索输出。当我从复选框中检查大小选项时,我想获得输出。如果我点击中号,我想显示所有中号连衣裙,喜欢所有 请给我一个解决方案
这是我的复选框
这是我的代码
product/index.html.slim
= search_form_for @product_search, url: shop_index_path do |f|
= f.label :size_cont, "Size Available"
- StandardSize.all.each do |s|
= check_box_tag('product_search[standard_sizes_id_eq_any_cont][]', s.id )
= s.name
这是我的 ShopController.rb
class ShopController < ApplicationController
def index
@product_search = Product.ransack(params[:q])
@products = @product_search.result(distinct:true).page(params[:page]).per(8)
@product_search.build_sort if @product_search.sorts.empty?
end
型号standardsize.rb
class StandardSize < ActiveRecord::Base
belongs_to :product
end
这里是我的模型product.rb
class Product < ActiveRecord::Base
has_and_belongs_to_many :standard_sizes
end
这是我的服务器
Started GET "/shop?utf8=%E2%9C%93&q%5Bname_cont%5D=dress&q%5Bprice_paisas_gteq%5D=&q%5Bprice_paisas_lteq%5D=&product_search%5Bstandard_sizes_id_eq_all%5D%5B%5D=3&commit=Search" for 10.0.2.2 at 2015-07-23 10:12:42 +0000
Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by ShopController#index as HTML
Parameters: {"utf8"=>"✓", "q"=>{"name_cont"=>"dress", "price_paisas_gteq"=>"", "price_paisas_lteq"=>""}, "product_search"=>{"standard_sizes_id_eq_all"=>["3"]}, "commit"=>"Search"}
User Load (0.1ms) SELECT
用户.* FROM
用户WHERE
用户.
id= 1 ORDER BY
用户.
idASC LIMIT 1
除非您以其他方式配置 Ransack,否则所有搜索参数都应嵌套在 @q
下。我也用 _in
而不是 _eq_any_cont
.
替换
= search_form_for @product_search, url: shop_index_path do |f|
= f.label :size_cont, "Size Available"
- StandardSize.all.each do |s|
= check_box_tag('product_search[standard_sizes_id_eq_any_cont][]', s.id )
= s.name
和
= search_form_for @q, url: shop_index_path do |f|
= f.label :size_cont, "Size Available"
- StandardSize.all.each do |s|
= f.check_box :standard_sizes_id_in, {multiple: true}, s.id, nil
= s.name