Dimmerswitch
2007-Feb-16 01:21 UTC
using a variable to invoke a custom method within autocomplete
I''m implementing a version of the autocompletion script found at
http://ricardo.pacheco.name/blog/articles/2006/07/26/using-
autocomplete-with-belongs_to-fields and have run into a stumbling
block.
The issue involves passing in a variable to indicate which custom
method should be invoked. I have an ugly workaround (a duplicate
function hardcoded to use the custom method), but would like to learn
a better way to handle this case. It feels like I''m missing something
simple, but I''ll be damned if I can see it.
# from: app/helpers/application_helper.rb
def indexed_auto_complete_result(entries, entityType, field, index)
return unless entries
items = entries.map { |entry| content_tag("li", entry[field],
"id"
=> entityType+''::''+entry[index].to_s) }
content_tag("ul", items.uniq)
end
This works fine for referencing model attributes which exist in the
"Highlights" table (name, description, etc.) but fails if
''field'' is
passed in as "name_with_place". The unordered list is created, but
it''s full of empty <li> tags. If I change it to use entry.field I
get
NoMethodError (undefined method ''field'') errors in the log.
My eventual workaround was to add this function for those times I need
to access the name_with_place method. This works as desired, and
properly invokes the custom name_with_place method. It''s clearly not
DRY, though.
def indexed_auto_complete_result_with_place(entries, entityType,
field, index)
return unless entries
items = entries.map { |entry| content_tag("li",
entry.name_with_place, "id" =>
entityType+''::''+entry[index].to_s)}
content_tag("ul", items.uniq)
end
# from: app/models/highlight.rb
belongs_to :place
def name_with_place
if self.place.nil?
self.name
else
"#{self.name} (#{self.place.name})"
end
end
# from: app/controllers/highlights_controller.rb
def auto_complete_for_highlight_internal_name
find_options = {
:conditions => [ "LOWER(name) LIKE ?", ''%''
+params[:highlight]
[:internal_name].downcase + ''%'' ],
:order => "name ASC",
:limit => 10
}
@highlights = Highlight.find(:all,find_options)
render :inline => "<%= indexed_auto_complete_result @highlights,
''highlight_id'', ''name_with_place'',
''id'' %>"
end
# from: public/javascripts/application.js
function auto_complete_on_select(element, selectedElement)
{
var entityParts = selectedElement.id.split(''::'');
var entityType = entityParts[0];
var entityId = entityParts[1];
document.getElementById(entityType).value = entityId;
}
# from: app/views/highlights/index.rhtml
<% form_tag(''/highlights/edit'', :method => :post) do
%>
<%= text_field_with_auto_complete :highlight, :internal_name, { :size
=> 40 }, { :after_update_element =>
''auto_complete_and_redirect_on_select'' } %>
<%= hidden_field ''highlight'', ''id'' %>
<%= submit_tag("Edit") %>
<% end -%>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Not sure if this is what you want, but object.send(function_name) will do the trick for cases like that. Just be careful since I think .send will execute any function, public or private. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Dimmerswitch
2007-Feb-16 13:56 UTC
Re: using a variable to invoke a custom method within autoco
That works great for the dropdown. I''ll take a look at .send and the potential for executing any object method, which might make this approach a nonstarter. Thanks, Jon On Feb 15, 5:48 pm, Jon <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Not sure if this is what you want, but object.send(function_name) will > do the trick for cases like that. Just be careful since I think .send > will execute any function, public or private. > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---