简体   繁体   English

Twitter Bootstrap Typeahead gon gem并将数据从模型传递到JS - 以json格式

[英]Twitter Bootstrap Typeahead gon gem and passing data from model into JS - in json format

I would like the names of my 'firms'to be passed into an auto completing typeahead on a form. 我希望将我的“公司”的名称传递给表单上的自动完成预先输入。

I am using twitter bootstrap and the gon gem to pass the data into Json and make it available to the js. 我正在使用twitter bootstrap和gon gem将数据传递给Json并使其可用于js。

However i cannot get the field to respond, i dont think that the data is available to the form and do not understand why. 但是,我无法得到该字段的响应,我不认为该数据可用于表单,不明白为什么。

my firms controller does the following assigning the names of the firms to a gon variable. 我的公司控制器执行以下操作,将公司名称分配给gon变量。

gon.firms = Firm.all.map &:name

My Application.html.erb has the following in its header to make the gon variable available to js. 我的Application.html.erb在其标题中包含以下内容,以使gon变量可用于js。

<%= include_gon %>
<script type="text/javascript">
    jQuery(document).ready(function() {
        $(.typeahead).typeahead({source:gon.firms});
    });
    </script>

Then I have the form that I would like assign the typeahead to, I have assigned the class="typeahead" to it. 然后我有我想要指定typeahead的表单,我已经为它分配了class =“typeahead”。

<%= form_tag firms_path, :method => 'get', :class => 'typeahead'  do %>
<%= text_field_tag :search, params[:search], :class => 'input-medium search-query', :placeholder => 'Firm name' %>
<%= submit_tag "Search", :name => nil ,:class => 'btn' %>
<% end %>

Finally the application.js file the relevant jquery as per the twitter bootstrap documentation. 最后,application.js根据twitter bootstrap文档提供相关的jquery文件。

$(function() {  
    $('.typeahead .input-medium search-query').typeahead({source:[gon.firms]});
}

My application .js also has the following libraries 我的应用程序.js还有以下库

//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require twitter/bootstrap
//= require_tree .

I have checked that gon.firms is available as a variable, but the dev console has the following error 我已经检查过gon.firms可用作变量,但是开发控制台有以下错误

uncaught syntax error: Unexpected token

any ideas what may be causing this? 可能导致这种情况的任何想法?

Is there anything obviously wrong? 有什么明显的错误吗?

First make sure gon.firms is populated as you expect (check in your web dev tool console). 首先确保按预期填充gon.firms (检查您的Web开发工具控制台)。 Then while you're there make sure there aren't js errors as you type in the box. 然后,当你在那里时,确保在框中输入时没有js错误。 Finally, as per the docs , it looks like you want to attach the plugin to the text field, not the form: 最后,根据文档 ,您似乎想要将插件附加到文本字段,而不是表单:

$('.typeahead .search-query').typeahead( { source: gon.firms } );
This is because gon.firms is a ruby array not a js array. 这是因为gon.firms是一个ruby数组而不是js数组。

You can tranfer a string to js and then split it to a array like this: 您可以将字符串传递给js,然后将其拆分为如下所示的数组:

 $(function() { var col = "<%= @pon.firms.join(",") %>".split(","); $(".search-query").typeahead({ source: col }); }); 

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

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