babinho's weblog

Ruby and Coffee

Autocomplete Fields in Rails 3.1 With JQuery-UI Pt. 2

| Comments

As i have written in my previous post, i’m playing with autocompletion in my web app. I have found some new tricks, that will speed up the process. Never liked the find_by_name implementation, but as this example is derived from jquery.autocomplete plugin behavior, i had no other way. Did some more exploring today and here is what i found out. Because you are using json response on the ajax controller, and JQuery-UI autocomplete handles it perfectly, you can create a hidden field, and put the user_id there, so you have one less request to the database whena you are saving your record. The improved model:

app/models/post.rb
1
2
3
4
5
6
7
class Post < ActiveRecord::Base
  belongs_to :user
  attr_accessor :user_name
  def user_name
    user.name if user_id
  end
end

And the part in your view that i left out the last time, but this one is just better in my opinion:

1
2
3
< %= f.label :user_name %>
< %= f.text_field :user_name %>
< %= f.hidden_field :user_id %>

Also, you have to edit the CoffeScript that does the magic for you, so it will return the id into the hidden field:

1
2
3
4
5
$(document).ready ->
  $('#post_user_name').autocomplete
    source: "/ajax/users"
    select: (event,ui) ->
      $("#post_user_id").val(ui.item.id)

An easier way of doing it, and leaner on the database queries, at least one less.

Comments