Having multiple text_field_with_auto_complete in the same view
Posted by Jim Morris on Tue Oct 17 23:45:47 -0700 2006
I ran into a problem using text_field_with_auto_complete
in a view
where I wanted to have many of them created by an iteration. You can
use the :index option for the text_field
, but it doesn't carry over to
the various divs used in the AJAX calls.
is what you really want to do, just as for a regular text_field.
I googled around and found this bug
report for a fix to
text_field_with_auto_complete
that allows :index, as that fix does not
appear to be in the current stable release of Rails or on Edge rails, I just created a
my_text_field_with_auto_complete
and it worked like a charm!!
So I put this in app/helpers/application_helper.rb and simply use
my_text_field_with_auto_complete
in my views.
Here is what the controller side looks like...
Nice work. One problem I see. When the parameters get serialized, you end up with something like {"object"=>{"index"=>"value"}} in the params, which confuses the standard `auto_complete_for handler`.
Whoops, no. Actually it does work.
Good point I'll add the controller side of the code too, as it does work.
Can i ask how the responder knows which partial to render? Or does it render all name partials?
thanks much,h
kevin- it will render the partial called `_names.rhtml` in the relevant views directory. There is only one partial used in this case. Maybe I misunderstood your question?
Well done. Thank you.
thanks
i need a default value in text box when i came that page. and when i just delete the value manually and type another then the auto text will apppear
I Tried this example i couldnt able to list the details but i can able to retrieve the datas what would be the problem? plz help me guyz, one of the requirements to my project.
Can you email me the details? I don't really understand what problem you are having.
# Controller.
def user_auto_complete
name= params[:user].keys[0] # get index as its always only one at a time
responder_auto_complete_for_user_name(params[:user][name][:name])
end
private
def responder_auto_complete_for_user_name(value)
param= '%' + value.downcase + '%'
find_options= {
:conditions => [ 'name like ?', param ], :order => 'name ASC', :limit => 60
}
@names = User.find(:all, find_options)
render :partial => 'names'
#render :update do |page|
#page['user_1_name_auto_complete'].show
#page['user_1_name_auto_complete'].replace :partial => "names"
#end
end
# Application helper
module ApplicationHelper
def my_text_field_with_auto_complete(object, method, tag_options = {}, completion_options = {})
if(tag_options[:index])
tag_name = "#{object}_#{tag_options[:index]}_#{method}"
else
tag_name = "#{object}_#{method}"
end
puts "Tag Name = > " + tag_name
(completion_options[:skip_style] ? "" : auto_complete_stylesheet) +
text_field(object, method, tag_options) +
content_tag("div", "", :id => tag_name + "_auto_complete", :class => "auto_complete") +
auto_complete_field(tag_name, { :url => { :action => "auto_complete_for_#{object}_#{method}" } }.update(completion_options))
end
def auto_complete_text_one(object, method, tag_options = {}, completion_options = {})
if(tag_options[:index])
tag_name = "#{object}_#{tag_options[:index]}_#{method}"
else
tag_name = "#{object}_#{method}"
end
(completion_options[:skip_style] ? "" : auto_complete_stylesheet) +
text_field(object, method, tag_options) +
content_tag("div", "", :id => tag_name + "_auto_complete", :class => "auto_complete") +
auto_complete_field(tag_name, { :url => { :action => "user_auto_complete" } }.update(completion_options))
end
def auto_complete_text_two(object, method, tag_options = {}, completion_options = {})
if(tag_options[:index])
tag_name = "#{object}_#{tag_options[:index]}_#{method}"
else
tag_name = "#{object}_#{method}"
end
puts tag_name
(completion_options[:skip_style] ? "" : auto_complete_stylesheet) +
text_field(object, method, tag_options) +
content_tag("div", "", :id => tag_name + "_auto_complete", :class => "auto_complete") +
auto_complete_field(tag_name, { :url => { :action => "auto_complete_for_" + tag_name } }.update(completion_options))
end
end
# view file
<% 1.upto(3) do |legi| %>
<%=auto_complete_text_one( :user, :name, {:index => legi, :size => 20}) %><br/><br/><br/><br/><br/>
<% end %>
# Database design
The database contains a table called 'Users' and the fieldname is 'name'.
Can you find the probs on above code
The code in my article works fine, I use it. If you are having problems, then send me more information on what problems you are having, or send me your code. The email address is at the top of this page on the right.
What does your partial look like?
Maybe this?
`<ul>`
<% @names.each do|name|%>
<li><%= name %></li>
<%end%>
`</ul>`
The partial I usually use is somewhat more complex as I like to include the ID. However there is a default used which looks much like what you show above, and for the purposes of this article should be sufficient.
Just wanted to say thanks for this! It solved my problem completely (I had the same issue with autogenerated fields) and it works like a charm. This should replace the standard text_field_with_autocomplete for future releases of RoR
Just thought you know this has been fixed:
`http://dev.rubyonrails.org/attachment/ticket/5983/add_index_support_to_text_field_with_auto_complete.diff`