0

I want to display various JS alerts all in a row. Below is an example of what I want:

def exec
  render :js => "alert('Exec function has started');" and return

  if was_executed_successful?
    render :js => "alert('Congratz! You're the champion');" and return
  else
    render :js => "alert('Loser!');" and return
  end
end

The problem of the code above is that it only displays the first alert.

What can I do to display all of them?

James Chevalier
  • 10,604
  • 5
  • 48
  • 74
kamusett
  • 1,403
  • 1
  • 12
  • 22

2 Answers2

0

Only single thing is rendered from a controller action at a time, So you can modify your code like this:

def exec
 js = []
 js << "alert('Exec function has started')" 

 if was_executed_successful?
   js << "alert('Congratz! You're the champion')" 
 else
   js << "alert('Loser!')"
 end
 render :js => js * ";"
end
Rahul garg
  • 9,202
  • 5
  • 34
  • 67
0

Rahul is on the right track, but like he said, only one render per action, so I think it has to be

def exec
 js = "alert('Exec function has started');" 

 if was_executed_successful?
   js << "alert('Congratz! You're the champion')" 
 else
   js << "alert('Loser!')"
 end
 render :js => js
end

Also, that will show two alerts one right after the other; if you want to show progress of the exec function, the solution is more complicated.

bgates
  • 2,333
  • 1
  • 18
  • 12
  • I think what I want is to show progress as you said. It's doesn't interesting to show both alerts right after the other. The interesting is to show one when the function starts, and another after it finishes. The space of time between begin and end could be long. What can I find a way to show progress? Do you know? Thanks! – kamusett Jul 24 '13 at 20:36
  • 1
    In that case you want to move the bulk of the method into a background job. Read up on delayed_job, and to show progress, try [polling](http://stackoverflow.com/a/5712838/437361). – bgates Jul 24 '13 at 20:46