很棒的嵌套集 - 如何放置项目?

发布于 2024-12-18 02:32:27 字数 1477 浏览 3 评论 0原文

我有以下表结构,其中存储树项目:

| 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文