Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/timakin/autocompl

A light-weight autocomplete integration for Rails application.
https://github.com/timakin/autocompl

Last synced: 16 days ago
JSON representation

A light-weight autocomplete integration for Rails application.

Awesome Lists containing this project

README

        

Autocompl
====

Autocompl is the light-weight library that provides an autocomplete function to Rails app.

This gem depends on Vanilla JavaScript, not jquery.

See the repo of [autoComplete](https://github.com/Pixabay/JavaScript-autoComplete) to check the usage of javascript code.

## Installation

Include the gem on your Gemfile

```
gem 'autocompl'
```

Install it

```
bundle install
```

## Configuration

Add it to your app/assets/javascripts/application.js file

```
//= require autocompl
```

And, add it to your app/assets/stylesheets/application.css file

```
*= require autocompl
```

## Example

As an example, let's configure for embed an autocomplete to search `Product#name`.

At first, edit `routes.rb` to let your javascript to access to the database.

For an example, if you'd like to add an endpoint to `ProductsController`, write down this code.

```
resources :products do
get "autocomplete_endpoint", on: :collection
end
```

After that, add this to `products_controller.rb`

```
class ProductsController < ApplicationController
...
# This will define `autocomplete_endpoint` on ProductsController.
autocomplete product: :name
...
end
```

Finally, write the javascript code and HTML to generate an autocomplete form.

```
# products.js
$(document).ready(function() {
new autoComplete({
selector: 'input[name="productSearch"]',
source: function(term, response){
$.getJSON('/products/autocomplete_endpoint', { term: term }, function(data){
term = term.toLowerCase();
var matches = [];
for (i=0; i
...
```

In addition, if you'd search with product `maker` name, that was internationalized with `name_ja` and `name_en` columns,

rewrite your `products_controller.rb` like this.

```
class ProductsController < ApplicationController
...
autocomplete product: :name, maker: [:name_ja, :name_en]
...
end
```