I just started a Rails application and I had the following problem: I want to use jQuery user interface and autocomplete, and therefore used gem rails3-jquery-autocomplete. I also use formtastic for my rail forms.
I have two models:
Client:
class Customer < ActiveRecord::Base has_many :carts end create_table :customer do |t| t.column :lastname, :string t.column :forename, :string end
Basket:
class Cart < ActiveRecord::Base belongs_to :customer end create_table :cart do |t| t.column :costumer_id, :integer end
As explained in the gem document, I made an autocomplete configuration in my CartController: autocomplete :customer, :lastname, :full => true
In my opinion, with formatted code, it looks like this:
<%= semantic_form_for @cart do |f| %> <%= f.inputs do %> <%= f.input :customer, :as => :autocomplete, :url => autocomplete_customer_lastname_carts_path, :id_element => '#cart_customer_id' %>
The main question I have is how to deal with the id file. I want to say that I want to store the customer reference identifier in my cart model, not the name itself. I learned about the ability to use the parameter :id_element
to refer to the id input field to store the identifier in the form. I realized that there seemed to be a problem with the formatting combination, and found a potential solution.
I could do this with <%= f.input :customer_id, :as => :hidden %>
, but I really don't know what to write in the line: <%= f.input :customer, :as => :autocom...
Is: a customer the right solution for this? This approach would give me the following: "cart"=>{"customer"=>"26","customer_id"=>"0"}
Does anyone have an idea, maybe I donβt completely understand the whole: thing id_element ...