【问题标题】:Rails JSON request not reaching controller, most of the timeRails JSON 请求大部分时间没有到达控制器
【发布时间】:2014-08-08 20:17:24
【问题描述】:

我很难诊断以下问题。我有一些服务器端跟踪代码,所以当单击下载链接时,我们将 json 请求传递给控制器​​。这会触发服务器端跟踪调用。

以下代码在 Chrome 中的 Heroku 上运行良好。然而,当我们投入生产(mt)时,我们开始出现一些奇怪的行为。如果您在 Chrome 中单击一次下载,则不会进行跟踪。如果您单击两次,它会跟踪第二次单击。如果您使用 IE11,它会按预期工作,生成跟踪事件。如果您使用 Firefox,它永远无法用于生产或暂存。

我们玩了很多东西都没有结果。请帮忙。

Rails 3.2.19 和 Ruby 2.1.0

事物控制器

def show
    authorize! :view, @thing, :message => "You do not have access to this thing. Is your subscription current?"
    respond_to do |format|
      format.html { track_event(current_user, "Viewed thing", {name: @thing.name}) }
      format.json do 
        track_event(current_user, "Downloaded thing", {name: @thing.name})
        render json: { :success => "success" }
      end
    end
  end

事物展示视图

<div class="thing_label" data-container="<%= thing.name.parameterize %>-thing">
    <% if !thing.zip_file.blank? %>
        <%= link_to  (image_tag "cloud.png", height:'30', width:'30', :style => "vertical-align:top"), thing.zip_file, class: 'js-download-thing', title: "Download Thing", rel:"tooltip", data: {toggle:"tooltip", placement:"top", title:"Download Thing", thing_id: thing.id}  %>
        &nbsp;&nbsp;
    <% end %>
    <%= thing.name.upcase %>
</div>

应用布局

    </div>
    </main>
    <%= render 'layouts/tracking' %>
  </body>
</html>

部分跟踪

<script> $(function(){   $('.js-download-thing').click(function(){
    thing_id = $(this).data("thing-id")
    url = "<%= things_path %>/" + thing_id + ".json";
    $.get(url);   }) }) </script>

【问题讨论】:

  • PS - 我在不同的对象上使用几乎完全相同的模式,它工作正常。该对象的不同之处在于 Rails 服务器将下载的对象发送给用户,而不是将它们定向到外部链接。

标签: javascript ruby-on-rails ruby json ruby-on-rails-3


【解决方案1】:

该链接将用户带到一个新页面,不是吗?

所以跟踪调用需要在链接加载之前完成,否则不会被调用。

也许您的生产环境明显更快,所以调用没有时间加载?

我会 return false; 点击链接,然后通过该 ajax 请求的回调将用户发送到下载链接。

我可能完全不在这里,但根据我的经验,我不知道您可以在单击链接后持续处理 js 而不会在某种程度上停止它。

【讨论】:

  • 是的,我认为你是对的。我们试图避免必须完全通过服务器路由请求,以避免可能导致用户重复单击链接的延迟。
  • 您可以在单击链接时禁用该链接。或将其切换为加载指示器。我还建议使用谷歌分析事件进行这种跟踪,它们非常轻量级,可能会减少您的内部跟踪造成的任何延迟。
猜你喜欢
  • 1970-01-01
  • 2015-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-03
  • 1970-01-01
  • 2022-11-28
  • 1970-01-01
相关资源
最近更新 更多