尝试对 Rails 中的参数执行方法

发布于 2024-11-30 08:32:02 字数 2147 浏览 1 评论 0原文

我想编写一个循环遍历所有参数的方法,以确保它们不都是空白。

我的参数是:

params[:search][:company]
params[:search][:phone]
params[:search][:city]
params[:search][:state]
params[:search][:email]

我有这个方法:

def all_blank_check(params)
  array=[]

  params[:search].each do |key, value|
    array << value unless value.blank?
  end

  if array.count < 1
      return true
  else
     return false
  end
end

但是当我尝试像 all_blank_check(params) 这样的东西时,我收到以下错误:

NoMethodError (undefined method `all_blank_check' for #<Class:0x108c08830>):

我需要先将参数转换为数组吗?我不能对参数执行方法吗?

编辑-完整来源:

        def index
          @customers = Customer.search_search(params)
        end

def self.search_search(params) search_field = [] search_values = [] array = [] test = '' if !params[:search].nil? && all_blank_check(params[:search] if !params[:search].nil? && !params[:search][:company].blank? search_field << 'customers.company LIKE ?' search_values << "%#{params[:search][:company]}%" end if !params[:search].nil? && !params[:search][:city].blank? search_field << 'customers.city = ?' search_values << "#{params[:search][:city]}" end if !params[:search].nil? && !params[:search][:phone].blank? search_field << 'customers.phone_1 = ?' search_values << "%#{params[:search][:phone]}%" end conditions = [search_field.join(' AND ')] + search_values Customer.where(conditions).includes(:customer_contacts).limit(10) end end def all_blank_check(params) params[:search].each do |key, value| array << value unless value.blank? end if array.count < 1 return false end if array.count > 1 return true end end

I want to write a method that loops through all the params to make sure they aren't all blank.

My params are:

params[:search][:company]
params[:search][:phone]
params[:search][:city]
params[:search][:state]
params[:search][:email]

I have this method:

def all_blank_check(params)
  array=[]

  params[:search].each do |key, value|
    array << value unless value.blank?
  end

  if array.count < 1
      return true
  else
     return false
  end
end

But when I try something like all_blank_check(params) I get the following error:

NoMethodError (undefined method `all_blank_check' for #<Class:0x108c08830>):

Do I need to convert the params to an array first? Can't I perform a method on params?

Edit - full source:

        def index
          @customers = Customer.search_search(params)
        end

def self.search_search(params) search_field = [] search_values = [] array = [] test = '' if !params[:search].nil? && all_blank_check(params[:search] if !params[:search].nil? && !params[:search][:company].blank? search_field << 'customers.company LIKE ?' search_values << "%#{params[:search][:company]}%" end if !params[:search].nil? && !params[:search][:city].blank? search_field << 'customers.city = ?' search_values << "#{params[:search][:city]}" end if !params[:search].nil? && !params[:search][:phone].blank? search_field << 'customers.phone_1 = ?' search_values << "%#{params[:search][:phone]}%" end conditions = [search_field.join(' AND ')] + search_values Customer.where(conditions).includes(:customer_contacts).limit(10) end end def all_blank_check(params) params[:search].each do |key, value| array << value unless value.blank? end if array.count < 1 return false end if array.count > 1 return true end end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人的夜不怕黑 2024-12-07 08:32:02

您还可以使用更多 Ruby 风格的代码,如下

def self.all_blank?(params)
   params[:search].count{|key, value| !value.blank?} == 0
end

所示 :如果数字为0,则表示全部为空。
它避免了仅仅为了计数而创建新数组。

You can also use more Ruby-minded code like this:

def self.all_blank?(params)
   params[:search].count{|key, value| !value.blank?} == 0
end

This counts the values that are not blank; if the number is 0, it means all are blank.
It avoids creating a new array just for counting.

欢烬 2024-12-07 08:32:02

问题不在于 params 的类型,问题在于调用该方法的对象上不存在该方法 all_blank_check

您将其定义为实例方法,并尝试从类方法 search_param 调用它,但这是行不通的。

如果您想让 all_blank_check 成为一个类方法,您需要将定义更改为 def self.all_blank_check(params) - 与 search_param 相同。

The problem is not the type of params, the problem is that the method all_blank_check does not exist on the object you call it on.

You defined it as an instance method and you're trying to call it from the class method search_param, which won't work.

If you want to make all_blank_check a class method you need to change the definition to def self.all_blank_check(params) - same as search_param.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文