2013-09-23 170 views
-2
<script> 
    function check() 
    { 
    <% if !user_signed_in? %> 
    alert("Sign in to order"); 
    <%redirect_to :path=>new_user_session_path%> 
    <% else %> 
    <% if current_user.points < form.order_total.value %> 
    <%redirect :path=>credits_path%> 
    <% end %> 
    <% end %> 
    } 
</script> 

我已经使用新页面编写了这个命令。我已经写了这个回应一个onclick行动。我在这里做错了什么?我也试过redirect_to方法无法正常工作rails

<%redirect_to new_user_sessions_path%>并且也类似于另一个。我应该如何改变做这项工作?

我得到的错误是

undefined method `redirect_to' 
+0

使用方法如下:redirect_to“你的路径” –

+0

它仍然说未定义的方法'redirect_to' – Aravind

回答

2

可以使用window.location使用JavaScript重定向用户和使用路径佣工通过滑轨得到的位置。像这样Sthing

<script> 
     function check() 
     { 
     <% if !user_signed_in? %> 
      alert("Sign in to order"); 
      window.location = "<%= j new_user_session_path %>"; 
     <% elsif current_user.points < form.order_total.value %> 
      window.location = "<%= j credits_path %>"; 
     <% end %> 
     } 
    </script> 

请注意,Rails部分在发送到客户端之前被解析。所以if语句不会在客户端执行。