Giter Site home page Giter Site logo

simple_form_autocomplete's Introduction

simple_form_autocomplete

An easy way to add AJAX autocomplete inputs to your forms. Works with has_many, has_and_belongs_to_many, and belongs_to associations.

Example

Models

class Book < ActiveRecord::Base
  belongs_to :author
  has_and_belongs_to_many :categories
end

Form

<%= simple_form_for @book do |f| %>
  <%= f.input :author, :as => :autocomplete, :source => authors_path %>
  <%= f.input :categories, :as => :autocomplete, :source => categories_path %>
<% end %>

Controller

The AJAX call passes a query parameter, which should be used to determine the records to send back. The controller should return a JSON hash with 3 keys: query, suggestions, and data.

class AuthorsController < ApplicationController
  def index
    respond_to do |format|
      format.json {
        authors = Author.where('name LIKE ?', "%#{params[:query]}%").limit(20).order('name')
        render :json => { :query => params[:query], :suggestions => authors.map(&:name), :data => authors.map(&:id) }
      }
    end
  end
end

CategoriesController follows the same convention.

Assets

Don’t forget to include simple_form.autocomplete.js and simple_form.autocomplete.css!

Requirements

  • Rails 3.1
  • Simple Form 1.51
  • jQuery-Rails 1.0.14

Credits

Written by Paul Smith

Based on DevBridge’s Ajax Autocomplete for jQuery

simple_form_autocomplete's People

Contributors

elandesign avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

simple_form_autocomplete's Issues

not yet a gem?

Could not find gem 'simple_form_autocomplete (>= 0) ruby' in any of the gem sources listed in your Gemfile.

Dead project?

Is this project still working? I tried to use it, but I've got an error:

undefined method `macro' for nil:NilClass

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.