Heyo Andrea...
I save the node - then end up calling .move_to_child_of(parent_id).
My models have their own methods that end up calling a few of the BNS
methods.
This is copied and pasted directly from the plugin''s README
in acts_as_nested_set:
# adds a new item at the "end" of the tree, i.e. with child.left
max(tree.right) + 1
child = MyClass.create(:name => "child1")
# now move the item to its desired location
child.move_to_child_of my_item
You can use:
* <tt>move_to_child_of</tt>
* <tt>move_to_right_of</tt>
* <tt>move_to_left_of</tt>
and pass them an id or an object.
-Stephen
On Mon, Nov 3, 2008 at 11:49 AM, Andrea Jahn <anja-email1 at web.de>
wrote:
>
> Hi,
>
> I found out that the parent_id is a protected attribute. So I cannot write
> this attribute
> without using the API of BetterNestedSet. There might be a good reason for
> that.
>
> Because of that it is not possible to use a field in the form to select the
> parent_id (f.collection_select)
> of the model resource. I will find another way for the user to select the
> item, which should be the parent
> for the new item.
>
> Andrea
>
>
>
> Hi,
>
> I tried to make a simple example with BetterNestedSet and have a problem
> with the parent_id.
>
> Migration:
> ------------
> class CreateItems < ActiveRecord ::Migration
> def self.up
> create_table :items do |t|
> # nested set
> t.integer :root_id
> t.integer :parent_id
> t.integer :lft, :null => false
> t.integer :rgt, :null => false
>
> t.string :name, :limit => 255, :null =>
false,
> :default => ""
> t.string : description, :limit => 512, :null =>
false,
> :default => ""
> t.integer :hw_model_id, :null => false
> t.boolean :deleted, :null =>
> false, :default => false
>
> t.timestamps
> end
> end
>
> def self.down
> drop_table :items
> end
> end
>
> In the view for entering a new item there''s a selection box for
selecting
> the parent node (value = id).
>
> Request parameters posted to the action create:
> --------------------------------------------------------------
> {"commit"=>"Create",
>
"authenticity_token"=>"6815a1cad4c491f5a243263eafdd4275ba86697b",
> "item"=>{"name"=>"hdhdfhdfhfh",
> "hw_model_id"=>"996332897",
> "description"=>"",
> "parent_id"=>"11",
> "workaround_id"=>"11"}}
>
> Controller:
> -------------
> def create
> @item = Item.new(params[:item])
> p @item.parent_id <-------------------------- nil ?????
> p @item.workaround_id
> @parent = Item.find(@item.workaround_id)
> if @parent
> p "parent gefunden"
> @item.root_id = @parent.root_id
> end
> if @item.save
> if not @parent
> p "parent nicht gefunden"
> # this item has no parent, so it is the root of a new tree
> @item.update_attribute("root_id", @item.id)
> else
> @item.move_to_child_of @parent
> end
>
> flash[:notice] = ''Item was successfully created.''
> format.html { redirect_to(@item) }
> <------------------------ error: too few arguments ????
> format.xml { render :xml => @item, :status => :created,
:location =>
> @item }
> else
> format.html { render :action => "new" }
> format.xml { render :xml => @item.errors, :status =>
> :unprocessable_entity }
> end
> end
>
> Logfile:
> --------
> Parameters: {"commit"=>"Create",
>
"authenticity_token"=>"6815a1cad4c491f5a243263eafdd4275ba86697b",
"acti
> on"=>"create",
"controller"=>"items",
"item"=>{"name"=>"hdhdfhdfhfh",
> "hw_model_id"=>"996332897",
"description"=>"",
"parent_id"=>"11",
> "workaround_id"=>"11"}}
>
> [4;35;1mItem Columns (0.031000)[0m [0mSHOW FIELDS FROM `items`[0m
>
> WARNING: Can''t mass-assign these protected attributes: parent_id
> !!!!!!!!!!!!!!!!!!!!!!!!
> -------------------------
>
> I have installed the plugin
> svn://rubyforge.org/var/svn/betternestedset/tags/stable/betternestedset
> and I''m using Rails 2.1.0
>
> Thanks in advance
> Andrea
>
>
>
> Schon geh?rt? Bei WEB.DE gibt'' s viele kostenlose Spiele:
>
*http://games.entertainment.web.de/de/entertainment/games/free/index.html*<http://games.entertainment.web.de/de/entertai%0A+nment/games/free/index.html>
>
>
> _______________________________________________
> Betternestedset-talk mailing list
> Betternestedset-talk at rubyforge.org
> http://rubyforge.org/mailman/listinfo/betternestedset-talk
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://rubyforge.org/pipermail/betternestedset-talk/attachments/20081103/23296950/attachment-0001.html>