diff --git a/app/controllers/assets_controller.rb b/app/controllers/assets_controller.rb
index 913af3c..d136fcd 100644
--- a/app/controllers/assets_controller.rb
+++ b/app/controllers/assets_controller.rb
@@ -26,6 +26,8 @@ class AssetsController < ApplicationController
def new
@asset = Asset.new
@asset.ips.build
+
+ @ips = Ip.unused
respond_to do |format|
format.html # new.html.erb
@@ -36,6 +38,7 @@ class AssetsController < ApplicationController
# GET /assets/1/edit
def edit
@asset = Asset.find(params[:id])
+ @ips = Ip.unused + @asset.ips
@asset.ips.build
end
diff --git a/app/models/asset.rb b/app/models/asset.rb
index a49c28f..11276d8 100644
--- a/app/models/asset.rb
+++ b/app/models/asset.rb
@@ -1,5 +1,12 @@
class Asset < ActiveRecord::Base
has_many :ips, :dependent => :nullify
validates_presence_of :hostname, :colo, :cage, :vendor
- accepts_nested_attributes_for :ips
+ # accepts_nested_attributes_for :ips
+
+ def ips_attributes=(ip_addresses)
+ self.ips = []
+ ip_addresses.each do |index, ip|
+ self.ips << Ip.find(ip["id"]) if (ip["id"] && !ip["id"].empty?)
+ end
+ end
end
diff --git a/app/models/ip.rb b/app/models/ip.rb
index c3e5747..f140e52 100644
--- a/app/models/ip.rb
+++ b/app/models/ip.rb
@@ -2,4 +2,6 @@ class Ip < ActiveRecord::Base
belongs_to :asset
validates_presence_of :ip_addr
validates_uniqueness_of :ip_addr
+
+ named_scope :unused, :conditions => "asset_id IS NULL"
end
diff --git a/app/views/assets/_form.html.erb b/app/views/assets/_form.html.erb
index 802566b..2d54ed2 100644
--- a/app/views/assets/_form.html.erb
+++ b/app/views/assets/_form.html.erb
@@ -15,10 +15,10 @@
<dd class="form"><%= f.text_field :cpu %></dd>
<dt class="form"><%= f.label :ram %></dt>
<dd class="form"><%= f.text_field :ram %></dd>
- <% f.fields_for :ips do |i| %>
- <% content_tag :dt, :class => "form" do %><%= i.label :asset_id, "Ip:" %><% end %>
+ <% f.fields_for :ips do |ip_form| %>
+ <% content_tag :dt, :class => "form" do %><%= ip_form.label :asset_id, "Ip:" %><% end %>
<% content_tag :dd, :class => "form" do %>
- <%= i.select :asset_id, Ip.find(:all, :conditions => "asset_id IS NULL").map {|i| [i.ip_addr, i.ip_addr]}, :include_blank => true %>
+ <%= ip_form.collection_select :id, @ips, :id, :ip_addr, :include_blank => true %>
<% end %>
<% end %>
<dt class="form"><%= f.label :role %></dt>
diff --git a/app/views/assets/show.html.erb b/app/views/assets/show.html.erb
index 8fd732a..7b19a15 100644
--- a/app/views/assets/show.html.erb
+++ b/app/views/assets/show.html.erb
@@ -43,6 +43,9 @@
<%=h @asset.subrole %>
</p>
+<% @asset.ips.each do |ip| %>
+<p>IP: <%= ip.ip_addr %></p>
+<% end %>
<%= link_to 'Edit', edit_asset_path(@asset) %> |
<%= link_to 'Back', assets_path %>
\ No newline at end of file