dot
dot

Reputation: 496

Flash[:error] add more than one at the same time

I use 'nokogiri' among others to check the schema from some uploaded xml's. And i will print out all errors which occurs:

    xsd.validate(doc).each do |error|
      flash[:error] = error.message 
    end

If I do so, I see only the last added error, if more than one exists.

I find also find a similar question, about this problem rails-easy-way-to-add-more-than-one-flashnotice-at-a-time but the accepted solution dosen't work for me.

Thanks

Upvotes: 0

Views: 294

Answers (2)

jvnill
jvnill

Reputation: 29599

change the method to

flash[:error] = xsd.validate(doc).map(&:message).to_sentence

UPDATE

Using br tags to separate each error

flash[:error] = xsd.validate(doc).map(&:message).join('<br>').html_safe

Upvotes: 1

Nejuf
Nejuf

Reputation: 466

I find also find a similar question, about this problem rails-easy-way-to-add-more-than-one-flashnotice-at-a-time but the accepted solution dosen't work for me.

In what way doesn't it work for you?

You can add your own flash types like flash[:errors] and write your own helper methods for convenience.

def my_flash(type, message)
  flash[type] ||= []
  flash[type] += Array.wrap(message)
end

Then you can use an array or a string as the message, making it easy to pass multiple in, like so.

my_flash :errors, "name cannot be blank"
my_flash :errors, ["age must be greater than 17", "phone number is invalid"]

p flash[:errors]
#=> ["name cannot be blank", "age must be greater than 17", "phone number is invalid"]

Upvotes: 0

Related Questions