简体   繁体   English

Ruby on Rails jQuery与tokenInput gem的问题

[英]Ruby on rails jQuery issues with tokenInput gem

I'm working on a rails app and was trying to integrate this gem for a text field 我正在使用Rails应用,正在尝试将这个gem集成到文本字段中

https://github.com/exAspArk/rails-jquery-tokeninput https://github.com/exAspArk/rails-jquery-tokeninput

However, I get the same console error: ".tokenInput is not a function" as when I try to do this without the gem (manually adding the js and css to the pipeline). 但是,我收到相同的控制台错误:“。tokenInput不是一个函数”,就像我在没有gem的情况下尝试这样做(手动将js和css添加到管道)一样。 I have the gem installed correctly, and this in my _form.html.erb 我已经正确安装了gem,这在我的_form.html.erb中

<% names = [{name: "test", name: "another test name", name: "third name test 123"}] %>
  <%= f.label :user_tokens, "Debaters" %>
  <%= f.text_field :user_tokens, 'data-tokeninput' => {collection: names}.to_json %>

I have properly requires the javascript in my application.js and I do receive token-input.js in the browser so the scripts are loaded properly, but for whatever reason it seems to not recognize the tokenInput function. 我已经在我的application.js中正确地使用了javascript,并且在浏览器中也确实收到了token-input.js,因此脚本已正确加载,但是由于某种原因,它似乎无法识别tokenInput函数。

Is there something i'm missing with the asset pipeline or loading the proper gems? 资产管道或加载适当的宝石时我缺少什么? If someone has experience working with this gem or knows what I'm missing with including the proper javascripts, i'd greatly appreciate any help! 如果有人有使用此gem的经验,或者知道我缺少包括正确的javascript在内的内容,我将不胜感激!

Your variable names that contains data like array of hash is not properly formatted. 您的变量名称包含诸如哈希数组之类的数据,格式正确。

names = [{name: "test", name: "another test name", name: "third name test 123"}]

If you try to parse the above code, it will give you warning like: 如果您尝试解析上面的代码,它将给您类似的警告:

duplicated key at line 1 ignored: :name

Because of duplication of key " name ". 由于键“ 名称 ”的重复。

So, you should properly format it: 因此,您应该正确格式化它:

names = [{name: "test"}, {name: "another test name"}, {name: "third name test 123"}]

Then, you will have the collection of names . 然后,您将具有names集合

Like, what @dieuhd said. 就像@dieuhd所说的。

由于您输入的令牌收集数据错误,因此必须:

<% names =  [{name: 'test' }, { name: 'another test name' },{ name: 'third name test 123' }] %>

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

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