2

I have an app that uses remote links, jQuery functions to handle them, and partials that load (modal) after the jQuery is finished. (Rails 3.2.11, Ruby 1.93, jQuery 1.9, running in development environment )

99% of the time everything works fine - the partial appears in a modal form floating above the "parent" form with the background darkened and the parent form blocked from click and scroll actions.. Works well...

Every once in a while what happens is that this breaks.. After clicking on a (remote) link, the screen is darkened but no modal form..

The jQuery does work - I can see entries in the browser logs, can have alert notifications etc but the partial never appears.. There are no console errors from the browser nor any Rails errors displayed in the terminal window.. It seems as if the Jquery is executing but handoff to Rails / the partial is never made...

This sounds like some kind of caching issue... I've tried rake assets: clean to no avail. What I have done taken a backup that worked, and copied over the new code and everything is working again.. I haven't changed the CSS in quite a while so that doesn't seem to be an issue either.

Any idea as to what the problem might be?

The routes check out etc...

Here are my files:

view code:

<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

What could be causing this? Is my jQuery just not correct but a working version is cached? Not quite sure what to make of this issue...

1 Answers1

2

Try to use //= require jquery_ujs in application.js instead of //= require jquery, and if you use this line javascript_include_tag *js_libraries, :cache => 'cached_js', then you need to remove cache attributes.

Try to add this code in application.js:

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

Read these answers: answer_1, and answer_2.

Community
  • 1
  • 1
Mohamed Yakout
  • 2,868
  • 1
  • 25
  • 45