【问题标题】:redirect_to method fails to work rails [closed]redirect_to 方法无法正常工作 [关闭]
【发布时间】:2013-09-23 06:44:34
【问题描述】:
<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 动作。我在这里做错了什么?我也试过了

对于另一个也是类似的。我应该如何改变才能让它发挥作用?

我得到的错误是

undefined method `redirect_to'

【问题讨论】:

  • 这样使用:redirect_to "your path"
  • 它仍然说未定义的方法`redirect_to'

标签: javascript ruby-on-rails ruby-on-rails-4


【解决方案1】:

您可以使用window.location 使用javascript 重定向用户,并使用路径助手通过Rails 获取位置。像这样的

   <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 else 语句不会在客户端执行。

【讨论】:

    猜你喜欢
    • 2014-06-13
    • 2014-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-29
    • 2013-05-23
    • 2017-04-08
    相关资源
    最近更新 更多