使用 bootstrap3_autocomplete_input 和关联时如何在编辑表单中正确显示值
How to properly display value in edit form while using bootstrap3_autocomplete_input and association
我正在尝试使用以下 Rails gem https://github.com/maxivak/bootstrap3_autocomplete_input , together with https://github.com/plataformatec/simple_form
提供的 auto-complete/type-ahead 功能
如果我为表单中的新记录选择 "new" 操作,一切正常。我可以通过自动完成功能在输入字段中选择值。问题是如果我选择 "edit" 操作来编辑现有记录。在那种情况下,字段不会显示正确的值(由表单预先填写),但会显示如下内容:#<Airport:0x007f98b478b7a8>
即使在 "show" 操作中,我也能看到显示的正确值。
我尝试将 f.input 更改为 f.association,就像我在开始实施自动完成之前所做的那样,但这没有帮助。
Cargo 模型中的记录存储了正确的 airports_id 引用,我在 rails 控制台中手动检查了这一点。
问题是,如果我选择 "edit" 操作而不是某种参考,我如何才能获得表格预填的正确机场值。
Rails 4.1.7
我的代码是:
货运型号:
class Cargo < ActiveRecord::Base
belongs_to :airport
...
货物视图:
...
<%= f.input :airport, :as => :autocomplete, :source_query => autocomplete_airport_city_airports_url %>
...
机场模型:
class Airport < ActiveRecord::Base
has_many :cargos, :dependent => :destroy
attr_accessible :iata_code, :name, :city
validates :iata_code, :name, :city, presence: true
validates :iata_code, :uniqueness => { :scope => :name }
validates :iata_code, length: { is: 3 }, format: { with: /\A[a-zA-Z\d\s]*\z/ }
validates :name, :city, length: { minimum: 2, maximum: 128 }
def full_airport_name
"#{city} / #{iata_code}"
end
end
机场管制员
class AirportsController < ApplicationController
autocomplete :airport, :city, { :display_value => 'full_airport_name', :full_model=>true }
...
路线:
resources :airports do
get :autocomplete_airport_city, :on => :collection
end
用户 f.association
因为 rails 会自动查找 :name
而你没有,你必须这样定义它:
f.association :airport, label_method: :full_airport_name, value_method: :id........etc
其实我发现了问题。首先,我重构了 Airports model
,删除了除 name
之外的所有列,并使用从单独的字符串 IATA code / City
连接的数据重新播种名称列。在此之后,需要在模型中指定要显示的值。这简单地解决了这个问题:
class Airport < ActiveRecord::Base
has_many :cargos, :dependent => :destroy
attr_accessible :name
validates :name, presence: true
validates :name, :uniqueness => true
def to_s
name
end
end
这是描述,我以前第一眼看不懂,在原始文档这里 https://github.com/maxivak/bootstrap3_autocomplete_input 模型部分。
我正在尝试使用以下 Rails gem https://github.com/maxivak/bootstrap3_autocomplete_input , together with https://github.com/plataformatec/simple_form
提供的 auto-complete/type-ahead 功能如果我为表单中的新记录选择 "new" 操作,一切正常。我可以通过自动完成功能在输入字段中选择值。问题是如果我选择 "edit" 操作来编辑现有记录。在那种情况下,字段不会显示正确的值(由表单预先填写),但会显示如下内容:#<Airport:0x007f98b478b7a8>
即使在 "show" 操作中,我也能看到显示的正确值。
我尝试将 f.input 更改为 f.association,就像我在开始实施自动完成之前所做的那样,但这没有帮助。
Cargo 模型中的记录存储了正确的 airports_id 引用,我在 rails 控制台中手动检查了这一点。
问题是,如果我选择 "edit" 操作而不是某种参考,我如何才能获得表格预填的正确机场值。
Rails 4.1.7
我的代码是:
货运型号:
class Cargo < ActiveRecord::Base
belongs_to :airport
...
货物视图:
...
<%= f.input :airport, :as => :autocomplete, :source_query => autocomplete_airport_city_airports_url %>
...
机场模型:
class Airport < ActiveRecord::Base
has_many :cargos, :dependent => :destroy
attr_accessible :iata_code, :name, :city
validates :iata_code, :name, :city, presence: true
validates :iata_code, :uniqueness => { :scope => :name }
validates :iata_code, length: { is: 3 }, format: { with: /\A[a-zA-Z\d\s]*\z/ }
validates :name, :city, length: { minimum: 2, maximum: 128 }
def full_airport_name
"#{city} / #{iata_code}"
end
end
机场管制员
class AirportsController < ApplicationController
autocomplete :airport, :city, { :display_value => 'full_airport_name', :full_model=>true }
...
路线:
resources :airports do
get :autocomplete_airport_city, :on => :collection
end
用户 f.association
因为 rails 会自动查找 :name
而你没有,你必须这样定义它:
f.association :airport, label_method: :full_airport_name, value_method: :id........etc
其实我发现了问题。首先,我重构了 Airports model
,删除了除 name
之外的所有列,并使用从单独的字符串 IATA code / City
连接的数据重新播种名称列。在此之后,需要在模型中指定要显示的值。这简单地解决了这个问题:
class Airport < ActiveRecord::Base
has_many :cargos, :dependent => :destroy
attr_accessible :name
validates :name, presence: true
validates :name, :uniqueness => true
def to_s
name
end
end
这是描述,我以前第一眼看不懂,在原始文档这里 https://github.com/maxivak/bootstrap3_autocomplete_input 模型部分。