我有两个班级
class GpsPoint < ActiveRecord::Base
validates :longitude, :presence => true
validates :latitude, :presence => true
belongs_to :station
end
和
class Station < ActiveRecord::Base
validates :name, :presence => true,
:length => { :maximum => 50 }
validates :gps_point, :presence => true
has_one :gps_point
belongs_to :route
end
创建工作站时,gps点将从工作站分开输入,我希望能够从下拉列表中选择一个gps点。
如何创建包含所有gps点的下拉列表?
答案 0 :(得分:2)
<%= select('station', 'gps_point_id', GpsPoint.all.collect {|u| [u.name,u.id]}) %>
答案 1 :(得分:1)
结帐Formtastic。这是一个宝石,可以让你轻松创建表单并自动处理外键 - 让你创建一个下拉或单选按钮。
否则在rails选择助手上有一篇不错的文章:
http://shiningthrough.co.uk/Select-helper-methods-in-Ruby-on-Rails