尝试对 Rails 中的参数执行方法
我想编写一个循环遍历所有参数的方法,以确保它们不都是空白。
我的参数是:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还可以使用更多 Ruby 风格的代码,如下
所示 :如果数字为0,则表示全部为空。
它避免了仅仅为了计数而创建新数组。
You can also use more Ruby-minded code like this:
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.
问题不在于
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 methodall_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 todef self.all_blank_check(params)
- same assearch_param
.