我的表格结构如下:
| 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.
在一名控制人员中,我这样做:
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
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?