Rails loop with number_field params? -


i use 2 number_field numbers, , if minor_sum > minor, 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 enter 2 number 111,112 , create, it's shows

comparison of string array failed

and mark line:

if minor.present? && minor_sum.present? && minor < minor_sum

the following 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 means got params number_field not number? how yo solve it?

i tried .to_i, shows:

undefined method `to_i' ["112"]:array

mark line:

if minor.present? && minor_sum.present? && minor.to_i < minor_sum.to_i

minor string , minor_sum array of strings (it's not clear why it's array, may check name of input generated html). on right track, compare them current code, you'd need cast them integers:

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 debug , find why you're getting params[:minor_sum] value array, though.


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -