This relates to the new standalone typeahead that Twitter recently released, not the Bootstrap version, see Twitter Typeahead.js
I'm trying to integrate this into a rails app to lookup sub-categories from the db and I'm having trouble trying to get it to work.
I have a local version working with hard coded data that you can see here: http://jsfiddle.net/v7dJ4/1/embedded/result/
In my rails version I get no errors in the console when I search.
Here is my JS:
$(document).ready(function() {
$('input.typeahead').typeahead({
name: 'names',
prefetch: '/sub_categories/names.json',
limit: 10
});
});
If I navigate to http://jog.dev/sub_categories/names.json I get the valid json data so that part is working:
[["Migrations","Controllers","Models","Associations","Views","Tests"]]
I think my problem is with 'name'. In the docs: https://github.com/twitter/typeahead.js#datasets it mentions that name is the string that is used to identify the dataset. Do I need to inject this into the json?
Any help much appreciated.
I am not sure that it is a good idea to have a variable with the same
name as the method. At the least it could be confusing.
wildcard
- The pattern in the remote
URL that will be replaced with the user's query when a request is made. Defaults to%QUERY
.The second question was (could you not have looked back at the message
yourself?) what happens after the query, you said it does not work but
did not say what was wrong.
Ok, getting closer. I found this post which says if I remove the leading '%' in the query it will find entries that 'Begin' with what is typed. This didn't work for me though and my query follows the same format:SubCategory.where("name like ?", "#{params[:q]}%")
Sorry for not being so precise.
Processing by SubCategoriesController#names as JSONParameters: {"q"=>"a"}
SubCategory Load (0.8ms) SELECT "sub_categories".* FROM "sub_categories" WHERE (name LIKE 'a%')