· 8 years ago · Jun 10, 2018, 07:24 AM
1class Datatable < ActiveRecord::Base
2 set_primary_key "widget_name"
3 acts_as_tree :order => "widget_description", :foreign_key => "parent_widget"
4end
5
6------------------------------------------------------------------------------------
7
8 def new
9 @root = Datatable.new
10 end
11
12 def create
13 @root = Datatable.new(params[:root])
14
15 # debugger
16 if @root.save
17 flash[:notice] = 'Contact was successfully created.'
18 redirect_to :action => 'list'
19 else
20 render :action => "new"
21 end
22 end
23----------------------------------------------------------------------------------------
24
25<h1>New post</h1>
26
27<% form_for :root, @root, :url => {:controller => 'home', :action => 'create'} do |f| %>
28
29 <p>
30 <%= f.label :widget_name %><br />
31 <%= f.text_field :widget_name %>
32 </p>
33 <p>
34 <%= f.label :parent_widget %><br />
35 <%= f.text_field :parent_widget %>
36 </p>
37 <p>
38 <%= f.label :widget_description %><br />
39 <%= f.text_field :widget_description %>
40 </p>
41 <p>
42 <%= f.label :children_count %><br />
43 <%= f.text_field :children_count %>
44 </p>
45
46 <p>
47 <%= submit_tag 'Save' %>
48 </p>
49<% end %>
50
51----------------------------------------------------------------------------------------------
52ERROR MESSAGE
53
54
55 ActiveRecord::StatementInvalid in HomeController#create
56
57Mysql::Error: Field 'widget_name' doesn't have a default value: INSERT INTO `datatables` (`widget_description`, `parent_widget`, `children_count`) VALUES('testwidget', 'Root', 0)
58
59------------------------------------------------------------------------------------------------
60DROP TABLE IF EXISTS `datatables`;
61
62CREATE TABLE `datatables` (
63 `widget_name` char(16) NOT NULL,
64 `parent_widget` char(16) default NULL,
65 `widget_description` varchar(100) NOT NULL default '',
66 `children_count` int(11) NOT NULL default '0',
67 PRIMARY KEY (`widget_name`)
68);
69
70LOCK TABLES `datatables` WRITE;
71
72INSERT INTO `datatables` VALUES ('Root',NULL,'Root',2);
73
74INSERT INTO `datatables` VALUES ('Child1','Root','Child 1',2);
75
76INSERT INTO `datatables` VALUES ('Child1.1','Child1','Child 1.1',0);
77
78INSERT INTO `datatables` VALUES ('Child1.2','Child1','Child 1.2',0);
79
80INSERT INTO `datatables` VALUES ('Child2','Root','Child 2',2);
81
82INSERT INTO `datatables` VALUES ('Child2.1','Child2','Child 2.1',0);
83
84INSERT INTO `datatables` VALUES ('Child2.2','Child2','Child 2.2',0);
85
86UNLOCK TABLES;