Hi,
in the view I have now:
<%= select_tag("parent_id",
"<option value=''0''>Please
select</option>" +
options_from_collection_for_select(@items, :id, :name)) %>
instead of:
<%= f.collection_select :parent_id, @items, :id, :name, :prompt =>
"Please select" %>
Then I can find the parent in the controller:
@parent = Item.find(params[:parent_id])
and add the parent (id) to the child with the help of one of the BNS methods
(and update lft and rgt):
@item.move_to_child_of @parent
Thanks
Andrea
*Von:* "Stephen Schor" <beholdthepanda at gmail.com>
*Gesendet:* 04.11.08 01:33:35
*An:* betternestedset-talk at rubyforge.org
*Betreff:* Re: [Betternestedset-talk] Problem with parent_id
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
[mailto: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 [http://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
[http://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 [http://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 [mailto:Betternestedset-talk at
rubyforge.org]
http://rubyforge.org/mailman/listinfo/betternestedset-talk
[http://rubyforge.org/mailman/listinfo/betternestedset-talk]
_______________________________________________Betternestedset-talk mailing
listBetternestedset-talk at
rubyforge.orghttp://rubyforge.org/mailman/listinfo/betternestedset-talk
"Run, Fatboy, Run" sowie "Rails & Ties" kostenlos
anschauen!
Blockbuster-Gutscheine sichern unter *http://www.blockbuster.web.de*
[http://www.blockbuster.web.de]
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://rubyforge.org/pipermail/betternestedset-talk/attachments/20081106/839539c8/attachment.html>