1

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Hishalv
  • 3,052
  • 3
  • 29
  • 52

2 Answers2

2

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>
Manuel van Rijn
  • 10,170
  • 1
  • 29
  • 52
1

Use jQuery's document.ready handler by passing $(...) a function.

$(function() {
  $.jGrowl('Your transaction has been processed', { life: 10000});
});

This will prevent your code from executing until the DOM is loaded and jGrowl can be properly displayed.

Jim Mitchener
  • 8,835
  • 7
  • 40
  • 56
  • thanks, however the message pops up on every page, and i just need jgrowl to pop up after a redirect in rails. Sorry about the noob question i am still learning jquery. Can you explain a bit more. thanks again for your help. – Hishalv Dec 09 '11 at 11:05