Mike Oligny
2006-Jun-02 14:25 UTC
[Rails] pass extra value through text_field_with_auto_complete?
Hi, I''m wondering if it is possible to pass an extra value (which I will use to further restrict my query) using text_field_with_auto_complete? Any suggestions would be appreciated! -Mike
Daniel N
2006-Jun-02 14:41 UTC
[Rails] pass extra value through text_field_with_auto_complete?
I have used the following in one of my partials to get this fuctionality. I have written a custom auto_complete_for_project_name to handle the query. Note the :with option for the auto_complete_field method is text that is then interperated as javascript. The View <label for="customer">Customer</label> <%= text_field_with_auto_complete :customer, :name %> <label for="project">Project</label> <%= text_field :project, :name%> <div id="auto_complete_for_project_name" class="auto_complete"></div> <%= auto_complete_field "project_name", { :url => {:action => ''auto_complete_for_project_name''}, :update => "auto_complete_for_project_name", :with => "''customer_name='' + $(''customer_name'').value + ''&project_name='' + $(''project_name'').value" } %> And in the controller. I got this from the rails auto_complete_for definition (the first part of the original is commented out!) def auto_complete_for_project_name # define_method("auto_complete_for_#{object}_#{method}") do cust = Customer.find_by_name(params[:customer_name]) || Customer.new find_options = { :conditions => [ "name LIKE ? AND customer_id = ?", ''%'' + params[:project_name] + ''%'', cust.id ], :order => "name ASC", :limit => 10 } @items = Project.find(:all, find_options) render :inline => "<%= auto_complete_result @items, ''name'' %>" end I have no idea if this is the right Rails way but it works for me so far. Although if anyone out there can come up with something better I''m all ears :-) On 6/3/06, Mike Oligny <mike@schema.ca> wrote:> > Hi, > > I''m wondering if it is possible to pass an extra value (which I will > use to further restrict my query) using text_field_with_auto_complete? > > Any suggestions would be appreciated! > > -Mike > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060602/58fa900a/attachment.html
Mike Oligny
2006-Jun-02 15:53 UTC
[Rails] Re: pass extra value through text_field_with_auto_complete?
Daniel ----- wrote:> I have used the following in one of my partials to get this > fuctionality. I > have written a custom auto_complete_for_project_name to handle the > query. > Note the :with option for the auto_complete_field method is text that is > then interperated as javascript.This looks like it may do exactly what I need - thank you for the quick reply! -- Posted via http://www.ruby-forum.com/.