创建了一个 model,对应我现有的硬件资产,如下
create_table "hardwares", force: :cascade do |t|
t.string "AssetNum", null: false
t.integer "CPU", null: false
t.integer "Disk", null: false
t.integer "Mem", null: false
t.date "BuyingTime", null: false
t.date "DeployingTime", null: false
t.string "Adapter1IP"
t.string "Adapter2IP"
t.string "Adapter3IP"
t.string "Adapter4IP"
t.string "Adapter5IP"
t.string "Position", null: false
t.string "ServerType", null: false
t.string "ServerModel", null: false
t.boolean "PCorNot", null: false
t.string "ServiceNum"
t.string "QuickServiceNum"
t.boolean "Idle"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
然后做了个 controller 和对应的新建表单,用来保存数据
class HardwaresController < ApplicationController
def show
@hardware = Hardware.find(params[:id])
end
def new
end
def create
# render plain: params[:hardware].inspect
@hardware = Hardware.new(hardware_params)
@hardware.Adapter1IP=''
@hardware.Adapter2IP=''
@hardware.Adapter3IP=''
@hardware.Adapter4IP=''
@hardware.Adapter5IP=''
@hardware.save!
redirect_to @hardware
end
private
def hardware_params
params.require(:hardware).permit(:AssetNum, :CPU, :Disk,:Mem,:BuyingTime,:DeployingTime,:Position,:ServerType,:ServerModel,:PCorNot, :ServiceNum, :QuickServiceNum, :Idle)
end
end
我有一个美好的想法:无论在新建还是以后修改 hardware 记录的时候,保存前都检查一下五块网卡是不是都分配了 IP 地址,如果都没有,那就在这条记录的 Idle 上标注它是空闲,于是有了下面的代码:
class Hardware < ActiveRecord::Base
attr_accessor :Idle, :Adapter1IP, :Adapter2IP, :Adapter3IP, :Adapter4IP, :Adapter5IP
before_save { self.idle? }
validates :AssetNum, presence: true
validates :CPU, presence: true
validates :Disk, presence: true
validates :Mem, presence: true
validates :BuyingTime, presence: true
validates :DeployingTime, presence: true
validates :Position, presence: true
validates :ServerType, presence: true
validates :ServerModel, presence: true
validates :PCorNot, presence: true
def idle?
if ( self.Adapter1IP.empty? || self.Adapter2IP.nil? || self.Adapter3IP.nil? || self.Adapter4IP.nil? || self.Adapter5IP.nil? )
self.Idle = false
else
self.Idle = true
end
end
end
于是烦人的事情发生,居然不能保存…… Started POST "/hardwares" for 127.0.0.1 at 2015-07-17 19:45:24 +0800 Processing by HardwaresController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"UpMreG5ZvfvQDF5uJp9bX09Uz9nPNW fx9biD+FYnSiilP29uyQ5dN+Ufl3DsoiOBz+L5uU5JzlNk1bDF2YUU6Q==", "hardware"=>{"AssetN um"=>"11", "CPU"=>"11", "Disk"=>"11", "Mem"=>"11", "BuyingTime(1i)"=>"2015", "Buy ingTime(2i)"=>"7", "BuyingTime(3i)"=>"17", "DeployingTime(1i)"=>"2015", "Deployin gTime(2i)"=>"7", "DeployingTime(3i)"=>"17", "Position"=>"11", "ServerType"=>"11", "ServerModel"=>"11", "PCorNot"=>"true", "ServiceNum"=>"11", "QuickServiceNum"=>" 11"}, "commit"=>"创建该硬件资产"} (0.2ms) begin transaction (0.0ms) rollback transaction Completed 422 Unprocessable Entity in 14ms (ActiveRecord: 0.8ms)
ActiveRecord::RecordNotSaved (Failed to save the record): app/controllers/hardwares_controller.rb:18:in `create'
Rendered /home/thanatos/.gem/ruby/gems/actionpack-4.2.3/lib/action_dispatch/mid dleware/templates/rescues/_source.erb (16.5ms) Rendered /home/thanatos/.gem/ruby/gems/actionpack-4.2.3/lib/action_dispatch/mid dleware/templates/rescues/_trace.html.erb (2.2ms) Rendered /home/thanatos/.gem/ruby/gems/actionpack-4.2.3/lib/action_dispatch/mid dleware/templates/rescues/_request_and_response.html.erb (1.0ms) Rendered /home/thanatos/.gem/ruby/gems/actionpack-4.2.3/lib/action_dispatch/mid dleware/templates/rescues/diagnostics.html.erb within rescues/layout (31.2ms) Rendered /home/thanatos/.gem/ruby/gems/web-console-2.2.1/lib/web_console/templa tes/_markup.html.erb (0.3ms) Rendered /home/thanatos/.gem/ruby/gems/web-console-2.2.1/lib/web_console/templa tes/_inner_console_markup.html.erb within layouts/inlined_string (0.4ms) Rendered /home/thanatos/.gem/ruby/gems/web-console-2.2.1/lib/web_console/templa tes/_prompt_box_markup.html.erb within layouts/inlined_string (0.2ms) Rendered /home/thanatos/.gem/ruby/gems/web-console-2.2.1/lib/web_console/templa tes/style.css.erb within layouts/inlined_string (0.3ms) Rendered /home/thanatos/.gem/ruby/gems/web-console-2.2.1/lib/web_console/templa tes/console.js.erb within layouts/javascript (14.2ms) Rendered /home/thanatos/.gem/ruby/gems/web-console-2.2.1/lib/web_console/templa tes/main.js.erb within layouts/javascript (0.2ms) Rendered /home/thanatos/.gem/ruby/gems/web-console-2.2.1/lib/web_console/templa tes/error_page.js.erb within layouts/javascript (0.3ms) Rendered /home/thanatos/.gem/ruby/gems/web-console-2.2.1/lib/web_console/templa tes/index.html.erb (27.1ms)
求指导!非常感谢!