Create complex objects
I have two classes
class GpsPoint < ActiveRecord::Base
validates :longitude, :presence => true
validates :latitude, :presence => true
belongs_to :station
end
and
class Station < ActiveRecord::Base
validates :name, :presence => true,
:length => { :maximum => 50 }
validates :gps_point, :presence => true
has_one :gps_point
belongs_开发者_运维百科to :route
end
The gps points are enter separated from station when a station is created i want to be able select a gps point from a drop down list.
How can i create the dropdown list with all the gps points ?
<%= select('station', 'gps_point_id', GpsPoint.all.collect {|u| [u.name,u.id]}) %>
Check out Formtastic. It's a gem that lets you easily create forms and automatically deals with Foreign Keys really well - lets you create a drop down or radio buttons.
Otherwise there is decent article on rails select helpers:
http://shiningthrough.co.uk/Select-helper-methods-in-Ruby-on-Rails
精彩评论