我使用的是collection_select,它连接到我的机场模型。
索赔
belongs_to :departure_airport, :class_name => 'Airport', :foreign_key => 'd_airport_id'
belongs_to :arrival_airport, :class_name => 'Airport', :foreign_key => 'a_airport_id'机场
has_many :claims_form.html.erb
<%= collection_select :claim, :d_airport_id, Airport.order('name'), :id, :name, {:prompt => true} %>目前的下拉显示“曼彻斯特国际机场”(例如),但我想包括来自同一型号的其他字段名。
曼城曼彻斯特国际机场EGCC (预期结果)
MAN和EGCC都是国际航空运输协会( iata )和国际民航组织(民航组织)机场模型中的两列。
我将继续只保存airport_id,然而,为了显示的目的,在下拉列表中的其他信息将是很好的。
发布于 2016-06-01 15:02:00
您可以使用您希望显示的格式化字符串向Airport模型添加一个方法。类似于:
def formatted_name
"#{iata} | #{name} | #{icao}"
end然后将该方法传递给collection_select而不是:name。所以:
<%= collection_select :claim, :d_airport_id, Airport.order('name'), :id, :formatted_name, {:prompt => true} %>请参阅文档这里。所讨论的论点称为:text_method。
发布于 2016-06-01 15:03:49
以下内容将对您有所帮助:选择
基本上,您可以在Airport模型中创建一个新方法(在models/Airport.rb中):
def collection_select_nice_data
"#{iata} | #{name} | #{icao}"
end在_form.html.erb中,您使用新创建的collection_select_nice_data
<%= collection_select :claim, :d_airport_id, Airport.order('name'), :id, :collection_select_nice_data, {:prompt => true} %>https://stackoverflow.com/questions/37572252
复制相似问题