SnippetStash's tag auto_complete

Tagged with Rails Ruby

Language: Ruby

View as text

# Using
#
# http://github.com/rails/auto_complete/tree/master for auto_complete
# and
#http://agilewebdevelopment.com/plugins/acts_as_taggable_on_steroids for tagging.


# View

<p>
  <%= f.label :tag_list %><br />
	    <%= text_field_with_auto_complete :snippet, :tag_list, {}, 
	       {:with => "'tag=' + $('snippet_tag_list').value",
		   :method=>"get", :url => formatted_tags_url("js"), :tokens => ","} %>
</p>


# Controller


class TagsController < ApplicationController
	def index
	  respond_to do |format|
    
	    format.html{@tags = Snippet.tag_counts(:conditions => ["public = ?", true])}
	    format.js do
      
	      tag = params[:tag].downcase.split(",").last.strip
	      find_options = { 
	        :conditions => [ "LOWER(name) LIKE ?", '%' + tag+ '%' ], 
	        :order => "name ASC",
	        :limit => 10 }
      
	      @items = Tag.find(:all, find_options)

	      render :inline => "<%= auto_complete_result @items, 'name' %>"
      
      
      
	    end
    
	  end  
  
	end
end
Original snippet written by Brian Hogan
Last updated at 00:18 AM on Jul 07, 2008 by Brian Hogan

SnippetStash costs money to host and develop. The service is free for everyone to use
but if you found it useful please consider making a small donation.