Search code examples
jqueryruby-on-rails-3jgrowl

How to display jgrowl messages on page redirect


I have setup jgrowl and everything seems to working correctly. I currently have to press a button to trigger the message to appear in the top right side of the screen like this:

<input type="button" onclick="$.jGrowl('Your transaction has been processed', { life: 10000 });" href="javascript:void(0);" value="Default"/>

What i want is for the jgrowl message to now pop-up automatically when i redirect in my controller:

def ch_sum_total
    #....some processing stuff code....
    respond_to do |format|
        format.html { redirect_to(:back) }
        format.js {}
    end
end

How can i do this?


Solution

  • I would add the message you want to Growl into you action on the controller so you can see if you have to show the jGrowl.

    So something like this:

    def ch_sum_total
        #....some processing stuff code....
    
        flash[:notice] = 'Your transaction has been processed'
    
        respond_to do |format|
            format.html { redirect_to(:back) }
            format.js {}
        end
    end
    

    and on the :back page you have something like:

    <script>
      $(function() {
        <% if flash[:notice] %>
          $.jGrowl('<%= escape_javascript(flash.discard(:notice)) %>', { life: 10000});
        <% end %>
      });
    </script>