简体   繁体   English

带有Rails Cocoon gem的Jquery遮罩问题:每次创建新条目

[英]Rails cocoon gem issue with jquery mask: new entry created every time

I am using Cocoon 1.1.1 to nest phone fields for a Person model. 我正在使用Cocoon 1.1.1来嵌套Person模型的电话字段。 I wanted to have a input mask for the phone number, so I included jquery.maskedinput-1.3.min.js under the javascript folder. 我想要输入电话号码的掩码,因此我在javascript文件夹下包括了jquery.maskedinput-1.3.min.js

It seems that when I apply the mask, the parameters submitted to Rails is missing the id for the phone attributes: 似乎当我应用该蒙版时,提交给Rails的参数缺少电话属性的id

 "phones_attributes"=>{"0"=>{"number"=>"214-648-4417", "label"=>"Work", "_destroy"=>"", "id"=>''}}}

This creates new entries every time. 每次都会创建新条目。 At this point, I am not adding new phone numbers, just clicking on Update. 此时,我没有添加新的电话号码,只是单击“更新”。

This is my Coffee Script: 这是我的咖啡脚本:

jQuery.fn.addPhoneMasks = ->
  $('input[id*="phone"]').each ->
    $(this).mask("999-999-9999?")

jQuery ->
  $('div#phones').addPhoneMasks()

(Note: I would need to use insertion callbacks to apply the mask to newly inserted phone fields, but I am not there yet :) ) (注意:我将需要使用插入回调将掩码应用于新插入的电话字段,但我还没有到这里:))

I found my error: the mask should not use id*="phone" because the jquery plugin adds a value to the hidden fields of cocoon. 我发现了我的错误:掩码不应该使用id*="phone"因为jquery插件向茧的隐藏字段添加了一个值。

My solution is to make a class for the phone, and then use this to for the selection. 我的解决方案是为电话创建一个类,然后使用该类进行选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM