Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 提交rails表单时忽略空白字段_Ruby On Rails_Postgresql - Fatal编程技术网

Ruby on rails 提交rails表单时忽略空白字段

Ruby on rails 提交rails表单时忽略空白字段,ruby-on-rails,postgresql,Ruby On Rails,Postgresql,我正在使用几个下拉列表创建搜索,如果用户没有选择,它们将提交“”。我需要一个catchall,类似于SQL中的*作为默认值。即,如果下拉列表中有5个品牌,我希望默认查询为所有5个品牌。类似于Brand.where(Brand:ALL)的东西。提前谢谢 <%= select_tag(:brand, options_for_select(["Brand 1","Brand 2","Brand 3","Brand 4","Other"].map{ |num| [num,num] }),id: '

我正在使用几个下拉列表创建搜索,如果用户没有选择,它们将提交“”。我需要一个catchall,类似于SQL中的*作为默认值。即,如果下拉列表中有5个品牌,我希望默认查询为所有5个品牌。类似于Brand.where(Brand:ALL)的东西。提前谢谢

<%= select_tag(:brand, options_for_select(["Brand 1","Brand 2","Brand 3","Brand 4","Other"].map{ |num| [num,num] }),id: 'brand', prompt: 'Brand', class: "table") %>

像这样的东西怎么样:

product.rb

class Product < ActiveRecord::Base
  scope :by_brand, -> (brand) { where brand: brand }
  # put more scopes for the other drop-down boxes
end
类产品(品牌){其中品牌:品牌}
#为其他下拉框放置更多作用域
结束
产品控制器.rb

class ProductsController < ApplicationController
  def search
    @products = Product.all
    @products = @products.by_brand(params[:brand]) unless params[:brand].blank?
    # more filtering here
  end
end
class ProductsController
您可能希望
在您的选择中包含空白的
和一个
提示

select_tag(..., include_blank: true, prompt: 'All')
现在,下拉列表中的第一个条目将有一个空白值,并为其标签显示“全部”

然后,您需要确保在查询时不使用该条件,类似这样(您没有发布任何代码,所以我不知道您的模型):


到目前为止你有什么例子吗?这就是一个例子。因此,默认值为Brand,我希望所选值为ALL,因此它实际上不过滤任何内容。您可以为“ALL”设置另一个选项,并将选项分配给控制器中所需的属性吗?When create()将brand参数选择为“all”时指定为保存.brand.where(brand::all)之前所需的方式,因为(brand::all)不是有效的语句,所以我认为这不起作用。我不希望您执行查询。。。?提交表格时,将品牌参数指定为“全部”。然后在controller create()方法中,获取参数值,如果参数值为“all”,则按您所需的方式分配属性。然后让它继续保存。现场-我把它复杂化了。非常感谢。
class MyController ...

  def show
    @items = Item.all
    @items = @items.where(brand: params[:brand]) if params[:brand].present?
    @items = @items.where(size: params[:size]) if params[:size].present?
    @items = @items.where(year: params[:year]) if params[:year].present?
    @items = @items.where(color: params[:color]) if params[:color].present?
  end

end