【问题标题】:Rails / jQuery / Modal forms - Cache issue? sometimes they just stop workingRails / jQuery / Modal forms - 缓存问题?有时他们只是停止工作
【发布时间】:2014-08-06 02:31:07
【问题描述】:

我有一个应用程序,它使用远程链接、jQuery 函数来处理它们,以及在 jQuery 完成后加载(模式)的部分。 (Rails 3.2.11、Ruby 1.93、jQuery 1.9,在开发环境中运行)

99% 的情况下一切正常 - 部分以模态形式出现在“父”表单上方,背景变暗,父表单被阻止点击和滚动操作。运行良好...

每隔一段时间就会发生这种情况。点击(远程)链接后,屏幕变暗,但没有模态表单。

jQuery 确实可以工作 - 我可以在浏览器日志中看到条目,可以有警报通知等,但部分永远不会出现.. 浏览器没有控制台错误,终端窗口中也没有显示任何 Rails 错误.. 似乎好像 Jquery 正在执行但切换到 Rails / 部分从未完成......

这听起来像是某种缓存问题...我试过 rake assets: clean 无济于事。我所做的工作进行了备份,并复制了新代码,一切都恢复正常了。我已经有一段时间没有更改 CSS,所以这似乎也不是问题。

知道问题可能是什么吗?

路线结账等...

这是我的文件:

查看代码:

<div class="modal" class="modal-hidden">
    <div class="modal-content" class="modal-hidden"> 
    </div>
</div>


<%= link_to(activity["status"], {:controller => 'statuses', :action=>'edit', :id=>activity["object_id"]},:remote=>true,:class=>"edit")%>

news.js

$(document).ready(function() 
{
  // .... misc functions... 
    $('#newsfeed').on('click','.edit', function(evt) {

     -----------misc code that makes modal divs visible and sizes them - not relevant to issue------

      $(".modal-content").css({ top:0, left: dialogLeft, position:"fixed", display:"inline"});
      evt.preventDefault(); 
  });
  //  .... misc functions... 
});

edit.js

$('.modal-content').html("<%= j(render(:partial=> 'statuses/edit', :locals=>{:id=>:id, :activity_list_id=>:activity_list_id}).html_safe).html_safe %>");

_edit.html.erb

body style="background: #4B7399">
<table style="background:#ffffff">
<tr>
<td>
<h2>Edit Status</h2>
<%= form_tag({:controller => :statuses, :action => :update, :id=> @status.id}, {:method => :put, :remote=>true} ) do |f|%>
    <br></div>
<%= hidden_field_tag :id,  @status.id.to_s%><br>
Status: <%= text_area_tag :status, @status.status.to_s %><br>
<%= submit_tag "Update"   %>
<% end %>
<button class="close">Close it</button>

<%= render "layouts/modal" %>

development.rb

  config.serve_static_assets = true

这可能是什么原因造成的?我的 jQuery 是否不正确但缓存了工作版本?不太清楚如何解决这个问题...

【问题讨论】:

  • 首先想到:您的news.js 代码是否在 document.ready 块中?
  • 是的..忘记将其添加到上面的代码中..
  • 您是否偶然使用了引导模式?
  • What I have done taken a backup that worked, and copied over the new code and everything is working again.. 现在可以用了吗?你能区分工作和不工作的应用程序吗?
  • 您是否尝试过查看浏览器开发工具中的“网络”选项卡以确保最后一个 ajax 请求成功完成?

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


【解决方案1】:

尝试在application.js中使用//= require jquery_ujs而不是//= require jquery,如果使用javascript_include_tag *js_libraries, :cache =&gt; 'cached_js'这一行,则需要去掉缓存属性。

尝试在application.js中添加这段代码:

$(function(){
  $('a').attr('remote', 'true').attr('data-type', 'script');
});

阅读这些答案:answer_1answer_2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    • 1970-01-01
    • 2022-06-17
    相关资源
    最近更新 更多