I have two models which are linked in a has_one / belongs_to association; Computer and Ipv6Address respectively.
I have pre-populated the Ipv6Address table with all the entries that I want it to have, and I now need to have a drop-down list on the Computer new/edit forms to select an item from Ipv6 to associate it with. The reason for needing to pre-populate this is that there is a predefined range of IPv6's that computers can have associated to them, and I want it to be as easy as possible for an end user to just pick something blindly from a list without having to understand why they're doing it; but give anyone who does know what it is the opportunity to pick one they want.
Everything I've seen so far on this only seems to work when you are creating both objects at the same time on the new form and then subsequently editing them.
I've tried to set up my MVC's as per the examples I've found online, and by checkout out other stackoverflow questions on a similar topic.
Computer model:
class Computer < ActiveRecord::Base
has_one :ipv6_address
accepts_nested_attributes_for :ipv6_address
...
Ipv6Address model:
class Ipv6Address < ActiveRecord::Base
attr_accessible :computer_id, :ip_address
belongs_to :computer
...
Computer controller:
class ComputersController < ApplicationController
def new
@computer = Computer.new
@ipv6s = Ipv6Address.where('computer_id IS NULL').limit(5)
end
def edit
@computer = Computer.find(params[:id])
@ipv6s = Ipv6Address.where('computer_id = #{@computer.id} OR computer_id IS NULL').order('computer_id DESC').limit(5)
end
Computer new form:
<%= simple_form_for( @computer ) do |f| %>
<%= f.fields_for :ipv6_address do |v6| %>
<%= v6.input :ipv6_address, :collection => @ipv6s %>
<% end %>
<% f.button :submit %>
<% end %>
When this new form renders, there are no input fields for ipv6_address included, though no errors are thrown. I've checked the source of the rendered page and nothing references "ipv6".
If I change the fields_for to:
<%= f.fields_for :ipv6_address, @ipv6s do |v6| %>
Then I get multiple dropdown selection fields appearing on the page, one for every object in @ipv6s, each containing a full list of objects in @ipv6s.