0

I have an AJAX create action for a rails controller that will select the form and recreate it by doing a render partial of just the form. This way, the error messages get shown when the validation fails.

However, one of my text fields is using the jQuery TokenInput plugin, and when the ajax call redraws the form, the jQuery TokenInput plugin is no longer loaded on the form.

Is there any way to trigger rerunning of the corresponding assets/javascripts/.js.coffee file from the AJAX controller action javascript file?

Thanks in advance...

1 Answer 1

1

After the form is redrawn, you just need to rerun your Tokeninput script. The best way to do this would be to place your Tokeninput command into a function (so you don't repeat yourself) and then just call that function after the form render. For example, let's say you want a Dog model that has_many collars that you want tokenized:

dogs.js.coffee:

tokenizeDogCollars = ->
  $("#dog_collar_tokens").tokenInput "/dogs.json"
tokenizeDogCollars

create.js.erb:

<% if @dog.errors.any? -%> 
  ...
  $('#dog-form-container').empty();
  $('#dog-form-container').append('<%= j render("form") %>');
  tokenizeDogCollars();
  ...
<% else %>
  ... whatever you want to do on a successful save ...
<% end %>
Sign up to request clarification or add additional context in comments.

Comments

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.