Rails3-jquery-autocomplete link id with formtastic

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 ...

+6
source share
1 answer

Your migration also got the wrong column name.

 create_table :cart do |t| t.column :customer_id, :integer end 

ok i found this :)

so you need to do hidden_field with customer_id and tell autocomplete to update this input value. tag: update_elements => {}

 <%= autocomplete_field_tag "cart_customer_name", "" , autocomplete_customer_lastname_carts_path, :id_element => "#customer_customer_id", :update_elements => {} %><br /> <%= f.hidden_field :customer_id %> 

So: id_element is the id element that you want to update when the result is selected.

+2
source

Source: https://habr.com/ru/post/911030/


All Articles