Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.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 如何在RubyonRails中验证表单中至少存在一个字段_Ruby On Rails_Ruby_Validation_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 如何在RubyonRails中验证表单中至少存在一个字段

Ruby on rails 如何在RubyonRails中验证表单中至少存在一个字段,ruby-on-rails,ruby,validation,ruby-on-rails-4,Ruby On Rails,Ruby,Validation,Ruby On Rails 4,我有一个包含多个列的搜索模型。这些列中填充了app/views/searchings/index.html.erb中@search do | s |的通用表单。问题是,我在这个视图中有三个字段,每个字段都由自己的复选框激活。我想验证是否存在至少一个字段。我在没有验证的情况下进行了测试,当输入字段被禁用时,其值为nil。代码如下: app/models/search.rb 我尝试用包含每个字段的布尔值的数组验证是否至少存在一个字段,但它似乎不起作用。每次我在表单上输入值时,它都会给我错误。我不确定

我有一个包含多个列的搜索模型。这些列中填充了
app/views/searchings/index.html.erb中@search do | s |
的通用
表单。问题是,我在这个视图中有三个字段,每个字段都由自己的复选框激活。我想验证是否存在至少一个字段。我在没有验证的情况下进行了测试,当输入字段被禁用时,其值为
nil
。代码如下:

app/models/search.rb


我尝试用包含每个字段的布尔值的数组验证是否至少存在一个字段,但它似乎不起作用。每次我在表单上输入值时,它都会给我错误。

我不确定神奇的布尔检查(只是在属性名称中添加一个
)是否是这样工作的。我想试试这样的东西:

def has_a_field
  fields = [:tipo, :ubicacion_cont, :capacidad, :free_to, :free_from]

  unless fields.any? { |field| self.send(field).present? }
    errors.add(:base, 'Debes ingresar al menos one parameter de búsqueda')
  end
end
试试这个:-

def has_a_field
    arr = [self.tipo, self.capacidad, self.ubicacion_cont, self.free_to, self.free_from]
    arr_with_present_val = arr.select(&:presence)
    if arr_with_present_val.blank?
        self.errors.add(:base, 'Debes ingresar al menos one parameter de búsqueda')
        return false    
    end
end

我找到了另一种解决方案,效果很好。在我的控制器内
def create
我检查
params[:search]
是否为零。如果是,则显示警报消息

def create

  if params[:searching].nil?

    redirect_to new_searching_path, alert: 'Debes ingresar al menos un parámetro'

  else

    #do the new and save stuff

  end

end
在使用
byebug
并检查
def create
中的
params
中接收到的内容后,我最终得到了这个解决方案。
我知道这不是rails编程的好做法,但为了满足我的需要,它很有魅力。

可能是重复的,看起来你的代码应该适合我。我会尝试使用调试器来检查变量,看看问题出在哪里如果你想看到我的所有代码,你可以在这里下载
def has_a_field
  fields = [:tipo, :ubicacion_cont, :capacidad, :free_to, :free_from]

  unless fields.any? { |field| self.send(field).present? }
    errors.add(:base, 'Debes ingresar al menos one parameter de búsqueda')
  end
end
def has_a_field
    arr = [self.tipo, self.capacidad, self.ubicacion_cont, self.free_to, self.free_from]
    arr_with_present_val = arr.select(&:presence)
    if arr_with_present_val.blank?
        self.errors.add(:base, 'Debes ingresar al menos one parameter de búsqueda')
        return false    
    end
end
def create

  if params[:searching].nil?

    redirect_to new_searching_path, alert: 'Debes ingresar al menos un parámetro'

  else

    #do the new and save stuff

  end

end