我有一个订单模型和一个客户模型,以便订单属于客户。我正在使用rails-jquery-autocomplete gem,而我正在尝试使用belongs_to关联构建表单。我设法让客户从订单端正确保存,但是现在如果你在编辑期间查看表单,就会显示出来。
我很乐意展示客户名称。但我不知道从哪里开始。
这是表格中该部分的视图。
.panel.panel-default
.panel-heading
=f.label :customer
.panel-body
=f.autocomplete_field :customer, :autocomplete_customer_name, placeholder: 'Customer', id_element: "#customer_id"
=f.hidden_field :customer_id, id:"customer_id"
=link_to "new customer", new_customer_path, class: "new_customer_link"
这是表格的控制器:
class OrderssController < ApplicationController
autocomplete :customer, :name
before_action :set_order, only: [:show, :edit, :update, :destroy, :toggle]
...
def rfq_params
params.require(:order).permit(:customer_id, :due, :ship_date, :is_budgetary, :notes, :end_user, :application, :mandatory_due_date, :tag_list)
以下是相关型号:
class Order < ActiveRecord::Base
has_many :valves, :class_name => "Valve"
accepts_nested_attributes_for :valves, :allow_destroy => true
belongs_to :customer
class Customer < ActiveRecord::Base
has_many :orders
belongs_to :company
end
答案 0 :(得分:0)
所以我无法“正确”地工作,而是在订单中添加了customer_name属性。并改变了
=f.autocomplete_field :customer, :autocomplete_customer_name, placeholder: 'Customer', id_element: "#customer_id"
到
=f.autocomplete_field :customer_name, :autocomplete_customer_name, placeholder: 'Customer', id_element: "#customer_id"
这是不幸的,但它有理想的结果。