1

I am using Rails 4 and jQuery-File-Upload and have some problems with form validation and jQuery activation. After I submit a form if there are some errors inside form I render form again inside my respond_to js.erb file like this:

<% if @cat.new_record? %>
    $('#new_cat').html('<%= j render('form') %>');
<% else %>
    ...
<% end %>

This displays all validation errors but at the same time disables jQuery-File-Upload which doesn't initialize again. Now when I want to submit a new form it doesn't work correctly and it just displays code after submit because no jQuery functions are present. What can I do to initialize jQuery again after I render the form again.

I tried to place this inside my coffeescript but no help:

$(document).on "ready page:load", initMyFunction

It doesn't record rendering of the form as the page load. Any ideas?

1 Answer 1

1

This sounds like it could be the problem with rails 4, jquery and turbolinks. Think of turbolinks as an ajax replacement of the entire page. It comes default with rails 4. The problem with it and jquery is your jquery ran and bound to stuff the first time the page was loaded... and is NOT rerun to bind to the new stuff on the next page "load". If you manually refresh your page and the problem stops it can be a sign of that.

There are two possible solutions. In the past I've taken the quick short term solution of just ripping out turbolinks Here's a link with instructions on how to do that:

http://blog.steveklabnik.com/posts/2013-06-25-removing-turbolinks-from-rails-4

Alternatively there is a gem that may help turbolinks play nice with your project. I haven't used it yet but it's discussed in this stackoverflow:

Rails 4 TurboLinks and jQuery Dynamic Links Not Playing Nice

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. That's definitely a problem! I don't want to remove turbolinks from my app so I am looking for some different solution. What do you think how can I register if the form is rendered again? I tried to listen on change on div which wraps the form but it doesn't register change of the div.
I'd start by trying the jquery-turbolinks gem mentioned in the other stack overflow question I linked above. I believe it keeps turbo links but works around the problem with them and jquery.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.