0

I am trying to make this behavior with my custom toastr notification

this is what i have

  def toast(type, text)
   flash[:toastr] = { type => text }
  end

I call this in my controller like this

toast('success',"this is a message")

and it would output to my template like so

        <% flash[:toastr].each do |type, message| %>
            toastr.<%= type %>('<%= message %>')
        <% end %>

however it only outputs 1 message

now here is the functionality I am trying to make, which is display multiple flash messages http://tomdallimore.com/blog/extending-flash-message-functionality-in-rails/

it works because of the following method

def flash_message type, text
  flash[type] ||= []
  flash[type] << text
end

everytime you call #flash_message, it would save the flash message in an array, and I can use for each on the array to display it.

I am having trouble converting my #toast into that, #toast CURRENTLY does this

flash[:toastr] = {'success' => "this is a message"}

I would like to do this

flash[:toastr] = [{'success' => "this is a message'},{'error' => "problem!"}]

Can someone help me modify toast method to accept an array of hashes, and inserts a new hash everytime its called?

2 Answers 2

1
def toast(type, text)
  flash[:toastr] ||= []
  flash[:toastr] << { type => text }
end
Sign up to request clarification or add additional context in comments.

Comments

0

Using Array#push:

def toast type, text
  flash[:toastr] ||= []
  flash[:toastr].push({ type => text })
end

Using append (<<):

def toast type, text
  flash[:toastr] ||= []
  flash[:toastr] << { type => text }
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.