@ywencn 谢谢!
下面是修改以后的,可以正常运行,不过不知道为什么没法写入到数据库里面
有两个模型 ip 和 sys
ip
class Ip < ActiveRecord::Base
has_many :syss
accepts_nested_attributes_for :syss
attr_accessible :ip_addr
end
sys
class Sys < ActiveRecord::Base
belongs_to :ip
attr_accessible :ip_id, :port, :server, :status
end
下面是视图
<%= form_for @add_dev, url: { controller: "home", action: "add" } do |f| %>
<%= f.text_field :ip_addr %>
<%= f.fields_for :syss do |sys| %>
<%= sys.text_field :server %>
<%= sys.text_field :port %>
<% end %>
<%= f.submit "Submit" %>
<% end %>
下面是控制器
def add
@add_dev = Ip.new
@add_dev.syss.build
if request.post?
@add_dev.save
redirect_to controller: "home", action: "index"
end
end
不知道它为什么不将数据定到数据库里面,日志里面写
Started POST "/home/add" for 127.0.0.1 at 2012-09-11 11:40:13 +0800
Processing by HomeController#add as HTML
Parameters: {"utf8"=>"_", "authenticity_token"=>"VNaISC/zY2KR603NUlgAQIzKsEnxs0/qrU9WxdGbqqc=", "ip"=>{"ip_addr"=>"192.168.1.1", "syss_attributes"=>{"0"=>{"server"=>"tete", "port"=>"90"}}}, "commit"=>"Submit"}
(0.2ms) BEGIN
SQL (0.5ms) INSERT INTO `ips` (`created_at`, `ip_addr`, `updated_at`) VALUES ('2012-09-11 03:40:13', NULL, '2012-09-11 03:40:13')
SQL (1.5ms) INSERT INTO `sys` (`created_at`, `ip_id`, `port`, `server`, `status`, `updated_at`) VALUES ('2012-09-11 03:40:13', 14, NULL, NULL, NULL, '2012-09-11 03:40:13')
(64.4ms) COMMIT
Redirected to http://192.168.186.129/home/index