Search code examples
ruby-on-railsrubyactionview

How to add more than one flash[:notice] at a interactive mode?


I'm using unobtrusive_flash and my view shows only the latest flash notices. How do I display all flash notices? Also, is it possible to display flash-notices interactively while the application is running?

I'm totally fine with any solution even if it doesn't use unobtrusive_flash.

view

<div class="unobtrusive-flash-container"></div>

controller

-------
while do
  begin
    config = {
      consumer_key:        '******',
      consumer_secret:     '*******',
      access_token:    '********',
      access_token_secret: '********'
    }
    rClient = Twitter::REST::Client.new config
    sClient = Twitter::Streaming::Client.new(config)
    topics = ['#trump', '#rails']
    sClient.filter(:track => topics.join(',')) do |tweet|
      if tweet.is_a?(Twitter::Tweet)
        puts tweet.text
        flash[:notice] = [tweet.text]
        flash[:notice] << tweet.text
        rClient.retweet tweet
      end
    end
  rescue
    puts 'error occurred, waiting for 5 seconds'
    flash[:error] = ['error occurred, waiting for 5 seconds']
    flash[:error] << 'error occurred, waiting for 5 seconds'
  end

Solution

  • flash[:notice] = [tweet.text]
    flash[:notice] << tweet.text
    

    should be

    flash[:notice] ||= [] # initialize a new array, the first time only
    flash[:notice] << tweet.text
    

    or you will reinitialize the flash array every time

    But to be honest, I think you should just look into: http://edgeguides.rubyonrails.org/action_cable_overview.html