Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Rails控制器接收所有参数,但无法正确创建数据库记录_Mysql_Ruby On Rails_Activerecord_Arel - Fatal编程技术网

Mysql Rails控制器接收所有参数,但无法正确创建数据库记录

Mysql Rails控制器接收所有参数,但无法正确创建数据库记录,mysql,ruby-on-rails,activerecord,arel,Mysql,Ruby On Rails,Activerecord,Arel,我要直截了当地说,根据我的日志文件,当我提交一个表单来创建一个新记录时,所有的参数都被正确发送了,但是在数据库中创建记录时,一些属性没有被设置。以下是相关的日志条目: Started POST "/computers" for 192.168.8.70 at 2011-05-19 16:24:31 -0400 Processing by ComputersController#create as HTML Parameters: {"utf8"=>"✓", "authenticit

我要直截了当地说,根据我的日志文件,当我提交一个表单来创建一个新记录时,所有的参数都被正确发送了,但是在数据库中创建记录时,一些属性没有被设置。以下是相关的日志条目:

Started POST "/computers" for 192.168.8.70 at 2011-05-19 16:24:31 -0400
  Processing by ComputersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"agR6MrYbMTbXeR9+Oit0rUzrhcKuhg6p/jpGqQD9MhI=", "show_location_buttons"=>"1", "computer"=>{"vendor_id"=>"1", "new_vendor_name"=>"", "model"=>"Hello", "hostname"=>"Hello", "user_id"=>"", "computer_type"=>"Desktop", "serial_number"=>"", "unh_id"=>"", "doc_id"=>"", "federal"=>"0", "department_id"=>"", "new_department_name"=>"", "security_id"=>"", "purchase_price"=>"", "purchase_date"=>"05/11/2011", "warranty_expiration"=>"05/28/2011", "activity_id"=>"", "new_activity_code"=>"", "condition_id"=>"", "new_condition_name"=>"", "location_id"=>"", "operating_system"=>"Fedora Core", "comments"=>""}, "commit"=>"Create Computer"}
  User Load (0.1ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 48 LIMIT 1
  SQL (0.1ms)  BEGIN
  SQL (0.4ms)  SHOW TABLES
  Computer Load (0.2ms)  SELECT `computers`.`id` FROM `computers` WHERE (`computers`.`hostname` = BINARY 'Hello') LIMIT 1
  Vendor Load (0.1ms)  SELECT `vendors`.* FROM `vendors` WHERE `vendors`.`id` = 1 LIMIT 1
  SQL (0.7ms)  describe `computers`
  AREL (0.3ms)  INSERT INTO `computers` (`vendor_id`, `model`, `hostname`, `computer_type`, `serial_number`, `unh_id`, `doc_id`, `federal`, `department_id`, `security_id`, `purchase_price`, `purchase_date`, `warranty_expiration`, `activity_id`, `condition_id`, `location_id`, `operating_system`, `license_sticker_id`, `comments`, `created_at`, `updated_at`, `user_id`, `delta`) VALUES (1, 'Hello', 'Hello', 'Desktop', '', '', '', 0, NULL, NULL, NULL, '2011-11-05', NULL, NULL, NULL, NULL, 'Fedora Core', NULL, '', '2011-05-19 20:24:31', '2011-05-19 20:24:31', NULL, 1)
如果仔细查看,“保修到期”的参数是在参数散列中设置的,但是如果查看日志中的AREL行,则插入数据库会删除保修到期字段的值并插入NULL。我完全被这件事吓呆了。有什么想法吗

以下是计算机资源的型号和控制器代码:

computer.rb

  class Computer < ActiveRecord::Base
    before_validation :format_fields

    validates :model, :presence => true
    validates :hostname, :presence => true, :uniqueness => true, :format => { :with => /^[a-zA-Z0-9]{3,25}$/ }
    validates :computer_type, :presence => true
    validates :operating_system, :presence => true

    validate :required_fields_specified

    belongs_to :vendor
    belongs_to :department
    belongs_to :security
    belongs_to :activity
    belongs_to :condition
    belongs_to :location
    belongs_to :license_sticker
    belongs_to :user

    has_many :hardware_addresses, :as => :addressable
    accepts_nested_attributes_for :hardware_addresses, :allow_destroy => true, :reject_if => lambda { |attr| attr[:mac_address].blank? }

    has_many :license_stickers, :dependent => :destroy
    accepts_nested_attributes_for :license_stickers, :allow_destroy => true, :reject_if => lambda { |attr| attr[:key].blank? or attr[:operating_system].blank? }

    has_many :pictures, :as => :imageable, :dependent => :destroy
    accepts_nested_attributes_for :pictures, :allow_destroy => true

    attr_accessor :new_vendor_name
    attr_accessor :new_activity_code
    attr_accessor :new_department_name
    attr_accessor :new_condition_name

    before_save :create_vendor_from_name
    before_save :create_activity_from_code
    before_save :create_department_from_name
    before_save :create_condition_from_name

    OPERATING_SYSTEMS = ["Microsoft Windows XP x86", "Microsoft Windows 7 x86", "Microsoft Windows Server 2003",
                         "Microsoft Windows XP x64", "Microsoft Windows 7 x64", "Microsoft Windows Server 2008 x86",
                         "Microsoft Windows Vista x86", "Microsoft Windows Vista x64", "Microsoft Windows Server 2008 x64",
                         "Ubuntu 8.x", "Ubuntu 9.x", "Ubuntu 10.x", "Fedora Core", "CentOS 5.x x86", "CentOS 4 x86", "IRIX",
                         "MacOS 10.5", "MacOS 10.6", "MacOS 10.7","CentOS 4 x64", "CentOS 5 x64", "ESX 3", "ESX 4", "SUSE"]

    COMPUTER_TYPES = ["Desktop","Laptop","Server"]

    def required_fields_specified
      errors.add_to_base "Specify an existing vendor, or create one." if vendor.blank? and new_vendor_name.blank?
    end

    def create_vendor_from_name
      create_vendor(:name => new_vendor_name) unless new_vendor_name.blank?
    end

    def create_activity_from_code
      create_activity(:code => new_activity_code) unless new_activity_code.blank?
    end

    def create_department_from_name
      create_department(:name => new_department_name) unless new_department_name.blank?
    end

    def create_condition_from_name
      create_condition(:name => new_condition_name) unless new_condition_name.blank?
    end

    def format_fields
      serial_number.upcase!
      hostname.capitalize!
      self.model = self.model.titleize
      unh_id.upcase!
      doc_id.upcase!
    end

    define_index do
      indexes vendor(:name), :as => :vendor_name, :sortable => true
      indexes model, :sortable => true
      indexes hostname, :sortable => true
      indexes computer_type, :sortable => true
      indexes serial_number, :as => :serial, :sortable => true
      indexes operating_system, :as => :os, :sortable => true
      indexes activity(:code), :as => :activity_code
      indexes condition(:name), :as => :condition_name
      indexes department(:name), :as => :department_name
      indexes license_sticker(:key), :as => :license_key
      indexes license_sticker(:operating_system), :as => :licensed_os

      has warranty_expiration, :as => :warranty
      set_property :delta => true
    end

  end
class计算机true
验证:主机名,:presence=>true,:university=>true,:format=>{:with=>/^[a-zA-Z0-9]{3,25}$/}
验证:computer_type,:presence=>true
验证:操作系统:存在=>true
验证:指定了必需的\u字段\u
属于:供应商
所属部门
属于:安全
属于:活动
属于:条件
属于:地点
属于:许可证标签
属于:用户
有多个:硬件地址,:as=>:可寻址
接受硬件地址的嵌套属性,允许销毁=>true,拒绝如果=>lambda{attr}attr[:mac_地址]。空白?}
有许多:许可证\u标签,:依赖=>:销毁
接受以下属性的嵌套属性:许可证标签、允许销毁=>true、拒绝如果=>lambda{attr}attr[:key]。blank?或attr[:operating_system]。blank?}
有许多:图片,:as=>:imageable,:dependent=>:destroy
接受\u嵌套的\u属性\u for:pictures,:allow\u destroy=>true
属性访问器:新的\u供应商\u名称
属性访问器:新的活动代码
属性访问器:新的部门名称
属性访问器:新的条件名称
保存前:从\u名称创建\u供应商\u
保存前:从\u代码创建\u活动\u
保存前:从\u名称创建\u部门\u
保存前:从\u名称创建\u条件\u
操作系统=[“Microsoft Windows XP x86”、“Microsoft Windows 7 x86”、“Microsoft Windows Server 2003”,
“Microsoft Windows XP x64”、“Microsoft Windows 7 x64”、“Microsoft Windows Server 2008 x86”,
“Microsoft Windows Vista x86”、“Microsoft Windows Vista x64”、“Microsoft Windows Server 2008 x64”,
“Ubuntu8.x”、“Ubuntu9.x”、“Ubuntu10.x”、“Fedora Core”、“CentOS 5.x x86”、“CentOS 4 x86”、“IRIX”,
“MacOS 10.5”、“MacOS 10.6”、“MacOS 10.7”、“CentOS 4 x64”、“CentOS 5 x64”、“ESX 3”、“ESX 4”、“SUSE”]
计算机类型=[“台式机”、“笔记本电脑”、“服务器”]
def必填字段\指定字段
errors.add_to_base“指定一个现有供应商,或创建一个。”如果vendor.blank?和新的供应商名称。空白?
结束
def根据供应商名称创建供应商
创建供应商(:name=>new\u vendor\u name),除非new\u vendor\u name.blank?
结束
def从_代码创建_活动_
创建活动(:code=>新建活动代码),除非新建活动代码为空?
结束
def根据部门名称创建部门
创建部门(:name=>new\u department\u name),除非new\u department\u name为空?
结束
def根据_名称创建_条件_
创建条件(:name=>new\u condition\u name),除非new\u condition\u name.blank?
结束
def格式\u字段
序列号。upcase!
hostname.capitalize!
self.model=self.model.titleize
哦,我的天哪!
文件号:upcase!
结束
定义索引do
索引vendor(:name),:as=>:vendor\u name,:sortable=>true
索引模型:sortable=>true
索引主机名:sortable=>true
索引计算机类型:sortable=>true
索引序列号,:as=>:serial,:sortable=>true
索引操作系统:as=>:os,:sortable=>true
索引活动(:代码),:as=>:活动\代码
索引条件(:name),:as=>:condition\u name
索引部门(:名称),:as=>:部门名称
索引许可证\u标签(:key),:as=>:许可证\u key
索引许可证标签(:操作系统),:as=>:许可证操作系统
保修期已到期,:as=>:保修期
set_属性:delta=>true
结束
结束
计算机\u控制器.rb

class ComputersController < ApplicationController
  filter_access_to :all

  # GET /computers
  # GET /computers.xml
  def index
    @computers = Computer.search params[:search], :order => sort_column("computer_type"), :sort_mode => sort_direction,
      :per_page => 20, :page => params[:page], :star => true

    respond_to do |format|
      format.html # index.html.erb
      format.js
      format.xml  { render :xml => @computers }
    end
  end

  # GET /computers/1
  # GET /computers/1.xml
  def show
    @computer = Computer.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @computer }
    end
  end

  # GET /computers/new
  # GET /computers/new.xml
  def new
    @computer = Computer.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @computer }
    end
  end

  # GET /computers/1/edit
  def edit
    @computer = Computer.find(params[:id])
  end

  # POST /computers
  # POST /computers.xml
  def create
    @computer = Computer.new(params[:computer])

    respond_to do |format|
      if @computer.save
        format.html { redirect_to(@computer, :notice => 'Computer was successfully created.') }
        format.xml  { render :xml => @computer, :status => :created, :location => @computer }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @computer.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /computers/1
  # PUT /computers/1.xml
  def update
    @computer = Computer.find(params[:id])

    respond_to do |format|
      if @computer.update_attributes(params[:computer])
        format.html { redirect_to(@computer, :notice => 'Computer was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @computer.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /computers/1
  # DELETE /computers/1.xml
  def destroy
    @computer = Computer.find(params[:id])
    @computer.destroy

    respond_to do |format|
      format.html { redirect_to(computers_url) }
      format.xml  { head :ok }
    end
  end

  private

  # def sort_column
  #   params[:sort] || "computer_type"
  # end

  # def sort_direction
  #   if params[:direction].nil?
  #     :asc
  #   else
  #     params[:direction].to_sym
  #   end
  # end
end
类计算机控制器sort\u列(“计算机类型”),:sort\u mode=>sort\u方向,
:per_page=>20,:page=>params[:page],:star=>true
回应待办事项|格式|
format.html#index.html.erb
format.js
format.xml{render:xml=>@computers}
结束
结束
#获取/computers/1
#GET/computers/1.xml
def秀
@computer=computer.find(参数[:id])
回应待办事项|格式|
format.html#show.html.erb
format.xml{render:xml=>@computer}
结束
结束
#获取/计算机/新
#GET/computers/new.xml
def新
@计算机
回应待办事项|格式|
format.html#new.html.erb
format.xml{render:xml=>@computer}
结束
结束
#获取/计算机/1/编辑
定义编辑
@computer=computer.find(参数[:id])
结束
#邮政/电脑
#POST/computers.xml
def创建
@计算机=计算机。新建(参数[:计算机])
回应待办事项|格式|
如果@computer.save
format.html{redirect_to(@computer,:notice=>“计算机已成功创建”)}
format.xml{render:xml=>@computer,:status=>:created,:location=>@computer}
其他的
format.html{render:action=>“new”}
format.xml{render:xml=>@computer.errors,:status=>:unprocessable_entity}
结束
结束
结束
#PUT/computers/1
#PUT/computers/1.xml
def upda
Computer.create! "vendor_id"=>"1", "new_vendor_name"=>"", "model"=>"Hello", "hostname"=>"Hello", "user_id"=>"", "computer_type"=>"Desktop", "serial_number"=>"", "unh_id"=>"", "doc_id"=>"", "federal"=>"0", "department_id"=>"", "new_department_name"=>"", "security_id"=>"", "purchase_price"=>"", "purchase_date"=>"05/11/2011", "warranty_expiration"=>"05/28/2011", "activity_id"=>"", "new_activity_code"=>"", "condition_id"=>"", "new_condition_name"=>"", "location_id"=>"", "operating_system"=>"Fedora Core", "comments"=>""
Open rails console --sandbox
$ computer = Computer.new
$  computer.model = 'abc'
$  computer.new_vendor_name = 'xyz'
$ Keep adding required column values.
$ now try computer.save
# if it prompts true, then your data has been saved successfully if you get false then you have error in your code.

$ computer.errors.full_messages

#above command will list out,where your failed the validations(added in Model).