Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tagging with autocomplete in Rails

My (long, I apologize) question is a follow-on to: How to add tagging with autocomplete to an existing model in Rails?

I am using acts-as-taggable-on and rails3-jquery-autocomplete, and trying to set up a system (much like Stack Overflow) where users begin to enter in a tag and suggestions appear in a drop-down box.

Goal

I am in the answers#new form and I want to see a list of tags that relate to questions. i.e. Imagine being on SO looking for new Rails questions to answer, and searching for ra. Ruby-on-Rails pops up, you click it, and you see a list of questions under RoR, any one of which you can answer.

These are the steps I've taken.

  1. Installed both gems. Both seem to work on their own.
  2. Added <%= javascript_include_tag "ui/jquery.ui.position", "ui/jquery.ui.autocomplete", "autocomplete-rails.js", "rails.js", "application.js" %>. (I already have Jquery, UI Core and UI Effects.)
  3. Answers controller: I added at top autocomplete :question, :tags, :full => true. I also tried autocomplete :tag, :name, :full => true.
  4. Question.rb: acts_as_taggable_on :tags.
  5. View:
    <%= form_tag new_answer_url, :method => "get" do %>
    <%= autocomplete_field_tag "tag_list", 'tags', autocomplete_question_tags_answers_path %>
    <% end %>

A simple autocomplete (no tagging) works (but it only works once per page load). With tagging, no success.

Problems

With lots of experimentation (and many hours) I am getting these problems:

  1. I get NameError (unitialized constant Tag) in server response to initial entry.
  2. With a non-taggable implementation (searching for the simple question text itself) I get a JQuery Autocomplete-style drop-down but my cursors cannot access the options with up/down. I have to click them with the mouse. Also, the dropdown doesn't disappear unless I reload the page!
  3. After the server responds with results once (only non-taggable is working as I mentioned), it doesn't respond again to key presses or changes in the text entry.

I would greatly appreciate any help you are able to give. I have gone through a few tutorials step-by-step but no luck today.

like image 975
sscirrus Avatar asked Apr 16 '11 08:04

sscirrus


1 Answers

I know it only answers one of your questions, but I was able to solve the "unitialized constant Tag" issue by explicitly specifying the class name in my controller:

autocomplete :tag, :name, :class_name => 'ActsAsTaggableOn::Tag'

It seems some of the changes to the acts_as_taggable_on library broke the underlying assumption that the Tag class exists.

Beyond that, I noticed some strange behavior myself when I didn't have my jquery-ui css correctly included on the page—have you verified that everything's getting linked in properly?

like image 173
Seth P Avatar answered Oct 05 '22 06:10

Seth P