Colton Seal
Colton Seal

Reputation: 379

Cannot flash errors array

I am having trouble flashing my errors array. Currently I am calling notice: errors, but that is returning an array that looks like this

["Please enter score range one.", "Please enter score range two.", "Please enter range between 1 and 100 for score one.", "Please enter range between 1 and 100 for score two."]

If I call flash: errors than I get this error

undefined method 'keys' for #<Array:0x0000010f196198>

What would be a better way of doing this? My code is below

if beg_score.present? && end_score.present? && beg_score.to_i.between?(1, 100) && end_score.to_i.between?(1, 100)
  CallLogByScoreWorker.perform_async(beg_score, end_score, query)
  redirect_to call_logs_path, notice: 'Calls were successfully made.'
else
  errors = []
  unless beg_score.present?
    errors << 'Please enter score range one.'
  end
  unless end_score.present?
    errors << 'Please enter score range two.'
  end
  unless beg_score.to_i.between?(1, 100)
    errors << 'Please enter range between 1 and 100 for score one.'
  end
  unless end_score.to_i.between?(1, 100)
    errors << 'Please enter range between 1 and 100 for score two.'
  end
redirect_to call_logs_path, flash: errors

Upvotes: 0

Views: 46

Answers (1)

Eric Mulligan
Eric Mulligan

Reputation: 151

Instead of:

redirect_to call_logs_path, flash: errors

do:

redirect_to call_logs_path, notice: errors.join(' ')

This way, it will return a string like the following:

'Please enter score range one. Please enter score range two. Please enter range between 1 and 100 for score one. Please enter range between 1 and 100 for score two.'

Check the RubyDocs documentation on the join method.

Upvotes: 1

Related Questions