RailsCasts Pro episodes are now free!
Learn more or hide this
There is a newer version of this episode, see the revised episode.
Resources
bundle rails g jquery:install
gem "jquery-rails"
class Book < ActiveRecord::Base attr_accessible :name, :author_tokens has_many :authorships has_many :authors, :through => :authorships attr_reader :author_tokens def author_tokens=(ids) self.author_ids = ids.split(",") end end
def index @authors = Author.where("name like ?", "%#{params[:q]}%") respond_to do |format| format.html format.json { render :json => @authors.map(&:attributes) } end end
<%= stylesheet_link_tag "application", "token-input-facebook" %> <%= javascript_include_tag :defaults, "jquery.tokeninput" %>
<p> <%= f.label :author_tokens, "Authors" %><br /> <%= f.text_field :author_tokens, "data-pre" => @book.authors.map(&:attributes).to_json %> </p>
$(function() { $("#book_author_tokens").tokenInput("/authors.json", { crossDomain: false, prePopulate: $("#book_author_tokens").data("pre"), theme: "facebook" }); });