auto_complete data and text field

two problems with auto_complete.

i have user table with id fname, lname,

following the steps on the wiki, drop down works but selected item does not appear in text box (issue #1)

i am searching only on fname but the drop down actually shows the full name by joining fname + " " + lname i would also like it to return the user_id which is what i really need to save in the db.

why is the selected answer not showing up and how i can get the id as well

def auto_complete_for_user_fname    auto_complete_responder_for_member params[:user][:fname] end

def auto_complete_responder_for_member(value)    @members = User.find......    render :partial => 'member_search' end

_member_search simply loops through @members

thanks

You may want to look at this:

yes, i looked at it and got even more confused because it was doing an associate

the simple auto complete works in regards to name appearing in text field, but not the customized. i thought this might be a css issue, but i have ruled that out as well.

# view Just a little test: <%= text_field_with_auto_complete :user, :lname %>

# controller auto_complete_for :user, :name

view: <%= text_field_with_auto_complete :user, :id %>

controller:

def auto_complete_for_user_id @users = User.find(:all, :conditions => [ 'LOWER(f_name) LIKE ?', '%'+params[:user][:f_name]downcase+'%' ], :order => 'f_name asc') render :partial => 'auto' end

partial _auto.html.erb:

<ul class="users"> <% for u in @users do -%>   <li class="user"><%=h u.id %><span class="informal"> (<%=h u.f_name%>)</span></li> <% end -%> </ul>

this should get a drop down menu of first names that puts the id in the text field

rushnosh wrote:

sorry, params[:user][:f_name].downcase

nope, that one doesn't even display a drop down.

here is my code from firefox. why does it say autocomplete off, is this the issue?

<input id="user_fname" type="text" size="30" name="user[fname]" autocomplete="off"/>

<script type="text/javascript"> 1 2//<![CDATA[ 3var user_lname_auto_completer = new Ajax.Autocompleter('user_lname', 'user_lname_auto_complete', '/user/auto_complete_for_user_lname/id', {}) 4//]]> 5 </script> <h1>Members</h1> <input id="user_fname" type="text" size="30" name="user[fname]" autocomplete="off"/> <div id="user_fname_auto_complete" class="auto_complete" style="position: absolute; left: 381px; top: 233px; width: 251px; display: none;"> <ul class="member_drop"> <li class="member_drop"> </li> <li class="member_drop selected"> </li> <li class="member_drop"> </li> <li class="member_drop"> </li> <li class="member_drop"> </li> <li class="member_drop"> </li> <li class="member_drop"> </li> <li class="member_drop"> </li> <li class="member_drop"> </li> <li class="member_drop"> </li> </ul> </div>

no, that's not the issue -- autocomplete off just tells the browser not to try to autocomplete the form

nothing's appearing because you don't have any data. make sure your database query works and it's populating the partial correctly. your ul class should be <ul class="members"> with each li being <li class="member">[name here]</li> or something similar to that format.

also you should probably make sure you have skip_before_filter :verify_authenticity_token, :only=>:auto_complete_for_user_id or whatnot there as well

rushnosh wrote:

there was data, because the drop down was working, it was just not selecting the name. not sure if anyone else has come across this. i was comparing the the two auto completes in firfox with firebug and i noticed in the simple auto complete post response from ajax was coming back all compacted (no spaces). the complicated auto_response had all the correct information but there were a lot of spaces.

so i deleted all the tabs and spacing from the partial, and it fixed the problem. i also noticed <%=h which i haven't come across before.

so that issue #1

i really need help with issue #2

i get the full name in the field, but i also want to get the id, even in another field. i would really appreciate a solution here. thanks. if i am not explaining my self please ask and i will repost.

Have a look at model_auto_completer:

    http://agilewebdevelopment.com/plugins/model_auto_completer

-- fxn

@xavier, i hope i can get this to work. plugin is installed, server restarted.

i read thru your docs.here is the error

undefined method `text_field_with_model_auto_completer' for #<ActionView::Base:0x230968c>

<%= text_field_with_model_auto_completer :user, :fname, :id, :my_id %>

undefined method `reflect_on_association' for NilClass:Class

i saw another example and tried but got the error above:

<%= belongs_to_auto_completer :user, :fname, :id %> is this suppose to create the text field?

Hi, the plugin does not define such helper, note that the docs are here:

    http://model-ac.rubyforge.org/classes/ModelAutoCompleterHelper.html

You normally use belongs_to_auto_completer.

-- fxn

Does that text field live in a form about a different model? Some model you dealing with that has a user_id?

-- fxn

never got it to work, i will look into later. it would be great if someone has small form code with this method, so i can look at it. it's the easiest way to learn it but there is nothing out there, i have searched hi and low.

never got it to work, i will look into later. it would be great if someone has small form code with this method, so i can look at it.

Besides the docs themselves, you have working examples in

    app/views/books/index.html.erb

checking out

    svn://rubyforge.org/var/svn/model-ac/trunk

Does that text field live in a form about a different model? Some model you dealing with that has a user_id?

Please, could you respond to that question?

-- fxn

@xavier

i don't have a form. i first created the auto complete with a text field, and looked at the code in firefox/firebug so i could understand it. the form i want to create is simple.

user table id fname lname

show all the users in the auto complete text field by full name. when a user click on a name, i want to get user_id field, either by ajax or with a submit button or with on enter.

Besides the docs themselves, you have working examples in

    app/views/books/index.html.erb

not sure on what you mean here? thanks for taking the time.

Hmmm, if there's no form to submit and no parent model let me suggest something that may be simpler: use Rails auto_complete plugin and put the user IDs as ids of the list items in the HTML completion list you send back.

    <li id="<%= user.id %>"><%=h user.name %></li>

Then, in the text_field_with_auto_complete helper call configure a JavaScript callback in :after_update_element that extracts the ID of the selected user and does whatever needed with it:

    function(element, value) {         var model_id = value.id;         ...     }

That's the basic technique under model_auto_completer, only it automates some additional stuff that you may not need for that simple use case.

-- fxn