Tagging with autocomplete in Rails
Asked Answered
N

2

7

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.

Nephrotomy answered 16/4, 2011 at 8:1 Comment(1)
I too am very curious to find how to best work this problem out.Ursine
B
4

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?

Bushmaster answered 25/4, 2011 at 19:42 Comment(0)
U
0

One thing I notice you're missing is anything your routes. I had to put something like this:

resources :resources do
  get :autocomplete_resource_tag, :on => :collection
end

In my _form.html.erb

<%= f.autocomplete_field :tag_list, autocomplete_resource_tag_resources_path %>

Now my problem is that the autocomplete still isn't loving me

SQLite3::SQLException: no such column: resources.tag: SELECT  resources.id, resources.tag FROM "resources"  WHERE (LOWER(resources.tag) LIKE 'woo%') ORDER BY resources.tag ASC LIMIT 10
Completed 500 Internal Server Error in 1ms

ActiveRecord::StatementInvalid (SQLite3::SQLException: no such column: resources.tag: SELECT  resources.id, resources.tag FROM "resources"  WHERE (LOWER(resources.tag) LIKE 'woo%') ORDER BY resources.tag ASC LIMIT 10):
Ursine answered 16/4, 2012 at 13:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.