Rails "The parameter passed to #in? must respond to #include?"
Rails "The parameter passed to #in? must respond to #include?"
作为此处另一个问题的跟进,我已将以下代码添加到我的 records_controller.rb
if params[:order].in? %s[year artist title]
# adds ORDER to the scope
@records.merge!( Record.order("? DESC", params[:order]) )
end
但是我得到一个错误:
"The parameter passed to #in? must respond to #include?"
这是什么意思?我需要改变什么?
%s[year artist title]
=> :"year artist title"
这是一个符号,我猜你想检查 param 是否是字符串之一。尝试使用 %w
代替。
if params[:order].in? %w[year artist title]
in?(another_object) public
Returns true if this object is included in the argument.
Argument must be any object which responds to #include?
include?
响应应为 %w[year artist title]
格式的对象数组,而不是像您编写的 符号 %s[year artist title]
%w[year artist title] # => ["year", "artist", "title"] can be used for include? and in?
%i[year artist title] # => [:year, :artist, :title] can be used for include? and in?
%s[year artist title] # => :"year artist title" which is a symbol that can not be used for include? or in?
另一种情况:
如果您的数组是 nil
,您将收到同样的错误
我遇到了同样的问题,但这是因为我没有为我的数组处理 nil
:
my_string.in? my_array
如果my_array = nil
我得到:(The parameter passed to #in? must respond to #include?)
为了解决这个问题,我刚刚更改了:
my_string.in? my_array || []
现在如果my_array = nil
我得到了假
作为此处另一个问题的跟进,我已将以下代码添加到我的 records_controller.rb
if params[:order].in? %s[year artist title]
# adds ORDER to the scope
@records.merge!( Record.order("? DESC", params[:order]) )
end
但是我得到一个错误:
"The parameter passed to #in? must respond to #include?"
这是什么意思?我需要改变什么?
%s[year artist title]
=> :"year artist title"
这是一个符号,我猜你想检查 param 是否是字符串之一。尝试使用 %w
代替。
if params[:order].in? %w[year artist title]
in?(another_object) public
Returns true if this object is included in the argument.
Argument must be any object which responds to #include?
include?
响应应为 %w[year artist title]
格式的对象数组,而不是像您编写的 符号 %s[year artist title]
%w[year artist title] # => ["year", "artist", "title"] can be used for include? and in?
%i[year artist title] # => [:year, :artist, :title] can be used for include? and in?
%s[year artist title] # => :"year artist title" which is a symbol that can not be used for include? or in?
另一种情况:
如果您的数组是 nil
我遇到了同样的问题,但这是因为我没有为我的数组处理 nil
:
my_string.in? my_array
如果my_array = nil
我得到:(The parameter passed to #in? must respond to #include?)
为了解决这个问题,我刚刚更改了:
my_string.in? my_array || []
现在如果my_array = nil
我得到了假