很棒的嵌套集 - 如何放置项目?
我有以下表结构,其中存储树项目:
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | YES | | NULL | |
| parent_id | int(11) | YES | | NULL | |
| lft | int(11) | YES | | NULL | |
| rgt | int(11) | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+------------+----------+------+-----+---------+----------------+
在我为树结构创建新项目的表单中,我在隐藏输入中始终是我刚刚创建的项目的父级。
在控制器中,我这样做:
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
save_tree = TreeStruct.create!(:user_id => @user.id)
save_tree.move_to_child_of(params[:parent])
format.html { redirect_to(root_url, :notice => 'OK.') }
format.xml { render :xml => @user, :status => :created, :location => @user }
else
format.html { render :action => "new" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end
但是当我尝试创建该树的项目时,我总是会收到错误:
Couldn't find TreeStruct with id=28
数字28是父项目的ID。当前创建的项目保存到数据库,列 parent_id 的值为 NULL。
我有点困惑——我做错了什么?为什么我会收到此错误?我应该如何创建树的根然后添加其他项目?
I have following structure of the table, where I store items of tree:
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | YES | | NULL | |
| parent_id | int(11) | YES | | NULL | |
| lft | int(11) | YES | | NULL | |
| rgt | int(11) | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+------------+----------+------+-----+---------+----------------+
In the form where I create a new items to the tree structure I have in a hidden input always the parent of item, that I just create.
In a controller I do this:
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
save_tree = TreeStruct.create!(:user_id => @user.id)
save_tree.move_to_child_of(params[:parent])
format.html { redirect_to(root_url, :notice => 'OK.') }
format.xml { render :xml => @user, :status => :created, :location => @user }
else
format.html { render :action => "new" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end
But always when I try to create and item to this tree, I will get the error:
Couldn't find TreeStruct with id=28
The number 28 is the ID od parent item. The currently created item saved to database, bud the column parent_id has the value NULL.
I am a bit confusing of it - what I am doing wrong? Why I am getting this error? And how I should create a root of the tree and then add other items?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论