Reputation: 397
I use two number_field to get numbers, and if minor_sum > minor
, I will create minor_sum - minor +1
objects.
<%= f.number_field :minor, :class => "form-control" ,placeholder: "(1~9999)" %>
<%= number_field :minor_sum, nil, :class => "form-control" , placeholder: "(1~9999)" %>
But when I enter two number 111,112 and create, It's shows
comparison of String with Array failed
and mark this line:
if minor.present? && minor_sum.present? && minor < minor_sum
the following is my controller:
def create
minor = params[:beacon][:minor] if params[:beacon][:minor].present?
minor_sum = params[:minor_sum] if params[:minor_sum].present?
if minor.present? && minor_sum.present? && minor < minor_sum.to_i
while minor < minor_sum
@beacon = Beacon.new(beacon_params)
...
minor += 1
end
else
...
end
end
Does it means that I got params from number_field is not number? How yo solve it?
I tried .to_i, shows:
undefined method `to_i' for ["112"]:Array
mark this line:
if minor.present? && minor_sum.present? && minor.to_i < minor_sum.to_i
Upvotes: 0
Views: 79
Reputation: 1082
minor
is a String
and minor_sum
is an Array
of String
s (it's not clear why it's an Array
, you may check the name of the input generated as HTML). You were on the right track, to compare them with the current code, you'd need to cast them to Integer
s:
minor = params[:beacon][:minor].to_i if params[:beacon][:minor].present?
minor_sum = params[:minor_sum][0].to_i if params[:minor_sum].present?
if minor.present? && minor_sum.present? && minor < minor_sum.to_i
...
I'd recommend that you debug and find why you're getting the params[:minor_sum]
value as an Array
, though.
Upvotes: 1