Auto_complete: if no results returned, what gets submitted?

Hi,

I’m a rails noob, tring to teach myself, so feel free to point me to
the correct FM if need be :wink:

I’m playing with the auto_complete plugin. I have a page to manage
expenses, and there’s field ‘supplier’ to whom the expense is paid.
I can successfully retrieve the suppliers name from the suppliers
table, but when I type a name that doesn’t exist yet, and submit,
nothing gets submitted?

Can anyone shed any light to why that is?
Ideally, in this situation, I’d like to be redirected to the supplier
model ‘new’ page, but I can settle for it just accepting what I type?

Best regards,
Wim

Wim,

I just tried it out, and it works fine for me – if I type a name that
doesn’t exist, then that name is submitted.

In the controller:

auto_complete_for :supplier, :name

In the action:

render :text => “Name = #{params[:supplier][:name]}”

And in the view:

<%= text_field_with_auto_complete :supplier, :name %>

If this doesn’t help you, maybe you could post some code?

/Lasse

2010/3/25 Wim Van D. [email protected]

Strange… What is the code you use to implement the autocomplete in the
view?

/Lasse

2010/3/30 Wim Van D. [email protected]

Hi Lasse,

thanks for repying.
I have the same for the controller and the view, not sure about the
action.

This is how set about to get there:
% script/generate scaffold_for_view supplier name:string street:string
streetnumber:decimal
zipcode:decimal city:string mail:string phone:string vat:string
% rake db:migrate
% gem sources -a http://gemcutter.org
% sudo gem install view_mapper
% ./script/plugin install git://github.com/rails/auto_complete.git
I add to the supplier model:
has_many :expenses

% script/generate scaffold_for_view expense title:string
amount:decimal payment:string
description:string category:string subcategory:string date:date –
view belongs_to_auto_complete:supplier

That’s it. When I add some suppliers, I can autocomplete them in the
expense form, but when I use a name that’s not found, it doesn’t get
submitted.
The output of the server script is:
Processing ExpensesController#create (for 127.0.0.1 at 2010-03-30
11:26:31) [POST]
Parameters: {“commit”=>“Create”, “expense”=>{“date(1i)”=>“2010”,
“subcategory”=>“test”, “category”=>“test”, “date(2i)”=>“3”,
“title”=>“Test”, “date(3i)”=>“29”, “amount”=>“0.0”, “payment”=>“test”,
“supplier_name”=>“tester”, “description”=>“test”},
“authenticity_token”=>“c845768fead94721c294f12ebc6f3b508e031398”}
Supplier Load (0.2ms) SELECT * FROM “suppliers” WHERE
(“suppliers”.“name” = ‘tester’) LIMIT 1
Rendering template within layouts/expenses
Rendering expenses/new
Rendered expenses/_form (78.5ms)
Completed in 183ms (View: 144, DB: 0) | 200 OK [http://localhost/
expenses]

Processing ExpensesController#create (for 127.0.0.1 at 2010-03-30
11:26:39) [POST]
Parameters: {“commit”=>“Create”, “expense”=>{“date(1i)”=>“2010”,
“subcategory”=>“test”, “category”=>“test”, “date(2i)”=>“3”,
“title”=>“Test”, “date(3i)”=>“29”, “amount”=>“0.01”,
“payment”=>“test”, “supplier_name”=>“”, “description”=>“test”},
“authenticity_token”=>“c845768fead94721c294f12ebc6f3b508e031398”}
Supplier Load (0.2ms) SELECT * FROM “suppliers” WHERE
(“suppliers”.“name” = ‘’) LIMIT 1
Expense Create (34.4ms) INSERT INTO “expenses” (“category”,
“subcategory”, “created_at”, “title”, “updated_at”, “amount”, “date”,
“payment”, “supplier_id”, “description”) VALUES(‘test’, ‘test’,
‘2010-03-30 09:26:39’, ‘Test’, ‘2010-03-30 09:26:39’, 0.01,
‘2010-03-29’, ‘test’, NULL, ‘test’)
Redirected to #Expense:0x103d8e920
Completed in 52ms (DB: 35) | 302 Found [http://localhost/expenses]

In the first block, the name is still there (tester), but in the
second, it isn’t. Why would that be?

Many thanks,
Wim