2013-07-24 27 views
0

我想連續顯示各種JS警報。下面是我想要的一個例子:如何使用Ruby on Rails在一行中顯示各種JS警報?

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 

上面的代碼的問題是,它只顯示第一個警報。

我能做些什麼來顯示所有這些?

回答

0

只有一件事情是從一個時間控制器動作渲染,所以,你可以修改你這樣的代碼:

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 
0

拉胡爾是在正確的軌道上,但他說的那樣,只有一個每個動作中渲染,所以我覺得它必須是

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 

同樣,將顯示一個右後其他兩個警報;如果要顯示exec函數的進度,解決方案會更復雜。

+0

我想我想要的是展現你的進步。將兩個警報正確地顯示在另一個之後並沒有意義。有趣的是在函數啓動時顯示一個,在完成後顯示另一個。開始和結束之間的時間間隔可能很長。我能找到一種展現進步的方式?你知道嗎?謝謝! – kamusett

+1

在這種情況下,您想將大部分方法移動到後臺作業中。閱讀delayed_job,並顯示進度,請嘗試[polling](http://stackoverflow.com/a/5712838/437361)。 – bgates

相關問題