views:

58

answers:

2

Hi,

I've recently installed this plugin, and I meant to create a Tag field with it, like StackOverFlow does.

When I put the following syntax on my AnnouncementsController(I want to tag announcements) it works great:

  auto_complete_for :announcement, :title
  protect_from_forgery :only => [:create, :delete, :update]

Also, I had to add the routes syntax as well to make it work:

map.resources :announcements, :collection => {:auto_complete_for_announcement_title => :get }

Now, when I try to accomplish the same with the tags, at the time I create a new announcement, I simply replace the word "announcement" for "tag" and "title" for "name", and it won't work. Tag makes reference for my Tags table at the database.

The error says the following:

<h1>   ActiveRecord::RecordNotFound

    in AnnouncementsController#show    </h1> 
<pre>Couldn't find Announcement with ID=auto_complete_for_tag_name</pre>

Can anybody tell me what I'm doing wrong?

Thanks, Brian

+1  A: 

In your view you probably want to change:

<%= text_field_with_auto_complete :announcement, :title %>

to:

<%= text_field_with_auto_complete :tag, :name %>

to make it work, take another look at the error it's giving, it's still calling announcement.

--- edit:

from autocomplete source:

 def text_field_with_auto_complete(object, method, tag_options = {}, completion_options = {})
makevoid
I don't understand something:what do the parameters for text_field_with_auto_complete refer to? And the same question for the auto_complete_for method.Because I'm trying to make it work for Tags, from my Announcements view.
Brian Roisentul
Can you post your view's code?
makevoid
The one that works is the following:Tags2: <%= text_field_with_auto_complete :announcement, :title, {}, {:method =>:get} %>And the one that does not work is the following:Tags: <%= text_field_with_auto_complete :tag, :name, {}, {:method =>:get} %>
Brian Roisentul
Check http calls with something like Firebug's Net tab.It seems that the text field observed by the js function is calling the wrong url: '/announcements/auto_complete_for_tag_name?params...'Have you defined: map.resources :tags, :collection => {:auto_complete_for_tag_name => :get } in your routes?
makevoid
Thanks. I guess this website has the clue for this. I have to read it carefully and test it.What do you think?URL : http://patshaughnessy.net/2009/1/30/sample-app-for-auto-complete-on-a-complex-form
Brian Roisentul
Yes, I do have that in my routes file. Should I use a fields_to :tag in my announcements' view and within there write f.text_field_with_auto_complete :tag, :name...?
Brian Roisentul
I realized that when I type something at that text_field, it goes to the Show method of the Announcements Controller, and the id is auto_complete_for_tag_name, so that's why I'm getting that error. The question is why it goes to that method if i'm at the New view, not showing anything!
Brian Roisentul
A: 

Well, I finally got the answer to my problem.

I was missing the following at routes.rb:

  map.auto_complete '/:controller/:action',
     :requirements => { :action => /auto_complete_for_\S+/ },
     :conditions => { :method => :get }

My new question now it works is the following:

What if I wanted to multi-tag an announcements, for example: "Ruby, C#". Should I change the plugin's logic or is there a functionality to make this work? Cause right now, it will check for the text_field text, not discriminating a new word after a comma or any kind of separator.

Thanks, Brian

Brian Roisentul
I could do this with "token => ','" after the "method" parameter in the view.
Brian Roisentul