autocomplete with multiple related form fields (in rails)

Monday September 8thTech Category

I’m an advisor and consultant for a social network site where you go upload images and tag the stuff you own and the stuff you want. One of the thing we wanted to do in the stuff-tagging form was to have related autocompletion fields. In this case, we want an autocompletion of brand names, and then based on the brand name input, we narrow down the autocompletion for models to only the models for that brand. This should be a pretty commonly done, but the auto_complete plugin in Rails has no easy way to do this.

The obvious approach was to use Rails’ autocompletion plugin to do the first field, and then use Scriptaculous directly for the second field, my first approach looked like this:

< %= text_field_with_auto_complete :product, :brand %> 
 
<input id="product_model" name="product[name]" size="30" type="text" />
<div class="auto_complete" id="product_model_auto_complete"></div>
 
<script type="text/javascript">
  new Ajax.Autocompleter(
    'product_model', 
    'product_model_auto_complete', 
    '/products/auto_complete_for_product_model', 
    { parameter: 'brand=' + $F('product_brand') }
  );
</script>

But this didn’t work because the parameters are populated on instantiation of the Ajax.Autocompleter, so dynamic form content are not properly sent to the controller. Took some googling and playing around to figure this out, but the solution is to append the additional parameter by overriding the callback.

< %= text_field_with_auto_complete :product, :brand %> 
 
<input id="product_model" name="product[name]" size="30" type="text" />
<div class="auto_complete" id="product_model_auto_complete"></div>
 
<script type="text/javascript">
  new Ajax.Autocompleter(
    'product_model', 
    'product_model_auto_complete', 
    '/products/auto_complete_for_product_model', 
    { callback: function(e, qs) {
        return qs + '&brand=' + $F('product_brand');
      }
    }
  );
</script>

Just for fun, I’m including the ajax endpoint to show how I generate the results in the desired unordered list format using the auto_complete_result helper. You should always specify a limit in these type of queries.

def auto_complete_for_product_model
  @results = Product.find_by_sql(
  [ %Q{select p.name from brands b, products p
      where lower(b.name) = ? 
      and p.brand_id=b.id 
      and lower(p.name) like ? limit 10}, 
    "#{params[:brand].downcase}", 
    "#{params[:product][:name].downcase}%" ]) if params[:brand] && params[:product][:name]    
    render :inline => "< %= auto_complete_result @results, 'name' %>"
end

BTW, this site is gonna make an appearance at the Techcrunch 50 demo pit, I should be there, check my Tweets, stop by and say hi if you happen to be around.

(the wp-syntax plugin adds a whitepsace between the < and the %, if you copy and paste my code you probably have to remove all the spaces for the erb to work.)

Tweets on 2008-09-07

Sunday September 7thTweets Category

  • @seoulfully is that the photograph I gave you in the background? :) #
  • got really sick yesterday, santa cruz was way too hot… woke up and feeling alright, good thing… #
  • Bringing Bear to yorkie meetup. #

Powered by Twitter Tools.

Tweets on 2008-09-06

Saturday September 6thTweets Category

Powered by Twitter Tools.

Looks the 5D replacement will finally be here soon…

Saturday September 6thPhotography Category

Even if it drops I’m most likely not gonna buy it any time soon. But I sure hope this thing is gonna beat the competition. That D700 is just really too damn good.

EVOLUTION
Uploaded with plasq’s Skitch!

Tweets on 2008-09-05

Friday September 5thTweets Category

  • it’s gonna hit 90 tomorrow here in sf, yikes! #
  • working from caffe sapore #
  • The Evo MR is sold. gonna go with buyer to his bank to get the loan check tmr. #
  • getting ready to bring Bear to doggy HH #

Powered by Twitter Tools.

Tweets on 2008-09-04

Thursday September 4thTweets Category

  • NeednCoffeenBadly #
  • Weird. Twitter didn’t like CR’s. #
  • @metroliner ya right? Those places look pretty damn nice too! #
  • doing some work from the apple store #
  • if you accidentally call ppl a lot on skype, please edit this setting, coz it’s very annoying… http://skitch.com/t/8e4 #
  • considering getting SARAHPALINISMOMJEANS.COM #
  • Internetz FAIL at the Serious Business office. #

Powered by Twitter Tools.

Flat Head party and discount at Self Edge

Thursday September 4th • Uncategorized Category

The president and VP of Flat Head, along with the editor-in-chief of Lightning Magainze are going to be in Self Edge this Sunday afternoon. I’m definitely gonna check this out. Info below:

We’re happy to announce that the men behind The Flat Head are coming to Self Edge from Nagano, Japan this Sunday for a party and product release.

The president and vice president of Flat Head are both coming from Japan along with the editor-in-chief of Lightning Magazine on Sunday, September 7th.

Self Edge
714 Valencia Street @ 18th Street
San Francisco
Sunday
September 7th
2pm to 6pm

Along with the party things that’ll be happening at this fine event:

  • The new Flat Head slim cut tapered jean, the SE05BSP, will be released
  • Self Edge x Flat Head Chambray Long Sleeve Shirts (navy, black, & red) with custom Kiryu Embroidery will be released.
  • Self Edge x Flat Head “Around the World” Loopwheeled T-shirts will be released
  • First 50 people to purchase anything in-store get a Red Flat Head Souvenir Bandana
  • A large selection of leather goods (wallets, belts…) and silver jewelry (rings, necklaces..) will be on display available for custom orders (this will be the only time to see these products in person and order them for a six week delivery)
  • Sidney Lo’s Flat Head 3001 WAYWT Exhibit will be on display including before/after jeans/shirts and the DVD of the progress

We’ll be serving 21st Amendment’s new Hell or High Watermelon Wheat and Brew Free! or Die IPA for those old enough to drink, along with a heavy dose of Flat Head’s favorite rockabilly music..

The “I am particular about my clothes” Sale:
15% off everything at Self Edge for those wearing any Flat Head item at the party (excluding the three new release items above and the leather/silver collection). For those that aren’t local, email us at denim@selfedge.com with a photograph of you wearing anything by Flat Head between Noon and 6pm (on Sunday) and we’ll send you an email back with a discount code good between Noon and 6pm the same day.

[From Self Edge Blog]

Tweets on 2008-09-03

Wednesday September 3rdTweets Category

  • At Mama’s again. This time with parents. #
  • now you can use XFBML is iframes, maybe iframe apps are the new way to go… http://tinyurl.com/5uwbnp #
  • warming up to HAML… #

Powered by Twitter Tools.

Tweets on 2008-09-02

Tuesday September 2ndTweets Category

Powered by Twitter Tools.

Tweets on 2008-09-01

Monday September 1stTweets Category

Powered by Twitter Tools.

Size

Colors