Rails 3.1 select can have object as value
Is it possibel to give select a complete Object as value.
Code:
Models app/models/pc.rb
class Pc < Device
has_many :gendevs
accepts_nested_attributes_for :gendevs, :reject_if => lambda { |a| a[:name].blank? },:allow_destroy => true
end
app/models/gendev.rb
class Gendev < Device
belongs_to :pc
end
(Gendevs are something like scanners, cardreaders etc). Given Gendevs are available in Database, I wanna have a nested select, giving me the attributes of an object.
View:
app/views/pcs/_form.html.erb
....
<% f.fields_for :gendevs do |gd开发者_JAVA技巧builder| %>
<%= gdbuilder.select :gendevs, @gendevs.map { |g| [g.name + '/' + g.kind,g] } %>
<% end %>
This is not working, so my question is, how can i do this. Is there a way to serialize the params of g, or how can i do this?
Use g.id instead of g.
<% f.fields_for :gendevs do |gdbuilder| %>
<%= gdbuilder.select :gendevs, @gendevs.collect { |g| [g.name + '/' + g.kind, g.id] } %>
<% end %>
精彩评论