format.js is not manipulating dom once action caching enabled
After some trial and error, I think I have a work around.
Page links
Instead of
$(function() { $('.pagination a').attr('data-remote', 'true') });
use
$(function() { $('.pagination a').click(function() { $.ajax({ url: this.href, dataType: 'script' }); return false; });});
so response created by the app server will be run as javascript
Controller
next, change your caches_action
line to
caches_action :index, cache_path: proc { |c| c.params.except(:_).merge(format: request.format) }
since ajax appends a _
params for some sort of timestamp
Hopefully this works :)
I don't see what the issue should be with using remote: true
. Someone else suggested to use .ajax
instead of remote: true
, but that's exactly what the remote functionality does, so there shouldn't be any difference.
The other answer has code that explicitly uses jQuery.ajax
, but the only difference in their code compared to what the remote functionality does is that they're specifying an explicit dataType
. You can actually do that with remote: true
though.
In your HTML link, you just need to specify data-type="script"
. Or, based on your posted JS, you'd do this:
$(function(){ $('.pagination a').attr('data-remote', 'true').attr('data-type', 'script');});
EDIT: Also, I wrote more in-depth about the data-type attribute and how it works with Rails here: http://www.alfajango.com/blog/rails-3-remote-links-and-forms-data-type-with-jquery/
I think I found a solution to this problem. I have a controller that has caches_action
for an action that uses format.js to fetch some data via ajax, and it was not working out of the box.
I found that, despite the request being transmitted to the server, and the server correctly parsing the request and "rendering" the index.js.erb
template, nothing was updating in the DOM. Your solution with $.ajax
and dataType:'script'
fixed the problem for me, however, I didn't like having to do jquery to bind to a click on a link, which should happen by default... I was able to make it work correctly by changing my link_to
to this:
= link_to "click me", user_action_path(params), remote: true, data:{type: 'script'}
Hope this helps!