text_field_with_auto_complete(object, method, tag_options = {},
completion_options = {})
Well, you can change the id with the tag_options hash, but the
generated javascript won't know about it. So, let's improve
text_field_with_auto_complete so that it does work.
def text_field_with_auto_complete(object, method, tag_options = {},
completion_options = {})
(completion_options[:skip_style] ? "" : auto_complete_stylesheet) +
text_field(object, method, tag_options) +
content_tag("div", "", :id =>
"#{object}_#{method}_auto_complete", :class => "auto_complete") +
auto_complete_field("#{object}_#{method}",
{ :url => { :action =>
"auto_complete_for_#{object}_#{method}" } }.update(completion_options))
end
The content_tag and the auto_complete_field methods both set the ids
of elements without consulting what id you passed to the tag_options
hash. We should change those two calls to match whatever :id option
was passed in, if in case it was passed in.
So...
def text_field_with_auto_complete_with_id_checking(object, method,
tag_options = {}, completion_options = {})
# lets just set the id now and not worry about it throughout
id = tag_options[:id] || "#{object}_#{method}"
(completion_options[:skip_style] ? "" : auto_complete_stylesheet) +
text_field(object, method, tag_options) +
content_tag("div", "", :id => "#{id}_auto_complete", :class =>
"auto_complete") +
auto_complete_field(id,
{ :url => { :action =>
"auto_complete_for_#{object}_#{method}" } }.update(completion_options))
end
To get this to work just place it in your application_helper.rb (and
call it with
text_field_with_auto_complete_with_id_checking(:person, :name, {:id =>
'bad_person'})
This seems to work for me but it doesn't have any tests written for it
as I just threw it together... I'll leave that to someone else.
Julian