Reputation: 1293
I've some problem. Can anyone help me with my mistake? I'm using rails 3 autocomplete gem from https://github.com/mikeauclair/rails3-jquery-autocomplete and follow all instructions, but have error:
undefined method `autocomplete_user_nickname' for #<Message:0xa09374c>
My routes:
resources :messages do
get :autocomplete_user_nickname, :on => :collection
end
MessagesController.rb:
autocomplete :user, :nickname
My new.html.erb:
<% form_for @message, :url => user_messages_path(@user) do |f| %>
<%= f.text_field :sender_id %>
<%= f.autocomplete_field :autocomplete_user_nickname, autocomplete_user_nickname_messages_path %>
<%= f.text_field :subject %>
<%= f.text_area :body %>
<%= submit_tag "Send" %>
<% end %>
What I am doing wrong? Thanks.
Upvotes: 1
Views: 8957
Reputation: 2477
Actually the :id_element
option is broken, and if you install from this github fork it will work
gem 'rails3-jquery-autocomplete', :git => 'git://github.com/juniorz/rails3-jquery-autocomplete.git'
Upvotes: 0
Reputation: 21
I think much better you use autocomplete.git. It works in Rails 3. It's easy.
https://github.com/rails/auto_complete/pull/9
But if you to know to use the jquery gem, I ask to you to put your solution and where WE are making mistake.
Bye....
Upvotes: 2
Reputation: 159095
According to the Readme at the project you linked, your form field is not correct. You have:
<%= f.autocomplete_field :autocomplete_user_nickname, autocomplete_user_nickname_messages_path %>
The Readme says it should be:
<%= f.autocomplete_field :user_nickname, autocomplete_user_nickname_messages_path %>
[Edit]
Based on your comments, I assume you have something like the following:
class Message < ActiveRecord::Base
belongs_to :user
end
class MessagesController
def new
@message = Message.new
end
end
I think you should use something like the following for your view:
<%= form_for(@message) do |f| %>
<%= f.hidden_field :user_id, :id => "real_user_id"
To: <%= autocomplete_field_tag 'throwaway_user', '',
autocomplete_user_nickname_messages_path,
:size => 75, :id_element => '#real_user_id' %> <br />
Subject: <%= f.text_field :subject %>
etc...
<% end %>
This way, the autocomplete field is just some throwaway data, but when it autocompletes it'll update the value of the real user ID tag (with the ID real_user_id
) to be the ID of the autocompleted user.
[Another Edit]
Since I wasn't sure what the problem was after your last comment, I decided to make a small app myself. It seems to be working fine for me, so I assume there's something up with your model.
You can see what I did to implement this at this GitHub compare view; I left out the boring stuff, like installing jQuery and jQuery UI. You can see the app actually working in the screenshots in the readme. Hope this helps.
Upvotes: 5