-
Notifications
You must be signed in to change notification settings - Fork 385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rails 4 + Turbolinks + Destroyed fields + Error on create / update #244
Comments
Good find. Can you make a pull request? |
If you like simple solutions 😎 |
I think #258 is better solution. |
I disagree. |
I don't think that |
The only part that should be in the |
Do you have any update and/or fix with cocoon and turbolinks ? |
@bricesanchez as I remember I made it work with |
@itsNikolay Could you provide me an example ? When i try to use it with turbolinks, It works when i do a complete refresh of the page. I'm on rails 4.2.0, it's an refinerycms app (3.0.0), i've disabled my custom JS to report this issue. This is my application.js manifest : //= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require turbolinks
//= require cocoon I've 2 forms in 2 pages (one by page). |
@bricesanchez sorry, I have been flooded with work so was not able to continue working on this. The only case where this error has effect is on invalid forms that are posted with deleted elements, and those are not hidden. So, imho, this is a bit of an edge case. The Simple work-around for now (if possible): disable turbolinks on that specific page by adding “data-no-turbolink” to the tag. I will try to update cocoon asap including this fix (e.g. somewhere around the weekend 🙏 ) |
@nathanvda, according to //= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//
// ... your other scripts here ...
//
//= require turbolinks Thanks for your reply and you're great job on this Gem! 😄 |
I ran into similar problems here, although more complex because I have a server side validation to ensure the last instance of the nested model cannot be removed (validates at least 1 of them present). This means the opposite of the above, because I need the destroyed fields to not be hidden if they specifically were the thing that failed validation I've solved it (seemingly) with the following: # _form.html.erb
<%= javascript_tag "resetCocoonDestroyed();" if @my_model.errors[:codes].present? %> // cocoon_form_reset.js
this.resetCocoonDestroyed = function() {
return $('.remove_fields.existing.destroyed').each(function() {
var el = $(this);
el.removeClass('destroyed');
return el.prev("input[type=hidden]").val("0");
});
}; This means any subsequent form submissions won't try to remove the element that I rejected. This is working with Turbolinks3, and without jquery.turbolinks, and I didn't have to add @nathanvda How are you coming with your overall solution? |
@nathanvda, not related to turbolinks, just basically cover destroyed fields hiding with document ready callback ^_^ #287 |
@Tensho , @nathanvda , @wyaeld Guys, has sby already solved this issue properly? I'm pretty beginner at JS, so for me it's not that easy to figure this out on my own. |
I fixed the edgecase, so cocoon is now completely turbolinks compliant and should now always work as expected. Only for @wyaeld there will be some impact: you might have to re-show the hidden fields 😦 |
Ok thanks @nathanvda I'll have a look next time I upgrade, that system using it doesn't get a lot of change, so should be fine. |
Thanks @nathanvda! I will upgrade right away! |
I still have this problem with Rails 5.1 and the last version of cocoon (1.2.11). |
@brianfalah which jquery version are you using? The Easy solutions (for now): use an older jquery version or use turbolinks. There is an open PR #447 to fix this, I will try to get it merged. |
Ok many thhanks @nathanvda. That is my problem, absolutely. Thanks, for your quick response |
With Rails 4 and Turbolinks, code is not reloaded / executed on resource creation fail as demonstrated in the documentation of Turbolinks :
This means that since cocoon.js is not wrapped in a function running on page:load / document.ready, the code is not executed on creation/update fail and the following function [ $(".remove_fields.existing.destroyed").each (i, obj) ->] does not run, thus destroyed_fields are not hidden
Simple fix would be to wrap the vendor js provided with the Cocoon gem with in :
The text was updated successfully, but these errors were encountered: