2016-03-14 43 views
0

我使用條紋在我的應用程序,有人把他們的信息後,我希望頁面的重定向到修改個人資料,這是edit_user_path(current_user)重定向在導軌上edit_user_path(CURRENT_USER)

我怎麼會觸發幾秒後重定向到該頁面?

我在想下面的JavaScript,但是我怎麼讓URL成爲用戶編輯路徑?

<script> 

$(document).ready(function() { 
    // Handler for .ready() called. 
    window.setTimeout(function() { 
     location.href = "https://www.google.co.in"; 
    }, 5000); 
}); 
</script> 

回答

1

雖然渲染你在服務器端視圖,您需要將edit_user_path(current_user)從紅寶石背景下出口到JavaScript的上下文。

有幾種方法可以將數據從ruby導出到javascript。的方法之一是使用javascript_tag,你可以如下(只是一個例子)分配給一個JavaScript變量:

<%= javascript_tag do %> 
    window.edit_current_user_path = '<%= edit_user_path(current_user) %>' 
<% end %> 

然後你可以使用該變量在JavaScript:

<script> 
$(document).ready(function() { 
    // Handler for .ready() called. 
    window.setTimeout(function() { 
     location.href = window.edit_current_user_path; 
    }, 5000); 
}); 
</script> 

參考JavaScriptHelper#javascript_tag獲取更多信息:

0

你可以在rails中創建一個帶有data屬性的隱藏元素。然後從jquery中讀取該標籤。

<div class='thing' data-thing='<%=edit_user_path(current_user)%>' style='display:none;'></div> 


$(document).ready(function() { 
    $('.thing').attr("data-thing"); //path 
});