views:

83

answers:

2

So I'm using the excellent Ancestry gem But while the documentation seems very complete I don't understand how to pass the parameter of my element which I want to be the parent of my newly created element. Firstly, do I want to do it in the new or create action... allow me to explain. For example: (with some actions removed for brevity)

class PeopleController < ApplicationController

  #...

  def new
    @person = Person.new
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      flash[:notice] = "Registration Successful."
      redirect_to root_url
    else
     render :action => 'new'
    end
  end
end

So namely I don't know where to create the ancestry, the docs say:

...You can use the parent attribute to organise your records into a tree. If you have the id of the record you want to use as a parent and don’t want to fetch it, you can also use parent_id. Like any virtual model attributes, parent and parent_id can be set using parent= and parent_id= on a record or by including them in the hash passed to new, create, create!, update_attributes and update_attributes!. For example:

TreeNode.create! :name => 'Stinky', :parent => TreeNode.create!(:name => 'Squeeky')

I want to know what my controller show look like to allow me to set the parent of the @person when I create them.

So otherwise I'm stuck, I don't know what else to do here... but anyhow, I do know that this gem is similar to the more popular acts_as_tree, any help is super appreciated!


Updated

I think I almost have it but when I try this for my create action

def create
  @parent = Recipe.find(params[:parent])
  @recipe = Recipe.new(params[:recipe], :parent => @parent.id) do |recipe|
    recipe.user_id = current_user.id
  end
  if @recipe.save
    current_user.has_role!(:owner, @recipe)
    redirect_to @recipe
  else
    render :action => 'new'
  end
end

I get:

Couldn't find Recipe without an ID


Updated

My view has a link to the new action that looks like this <%= link_to "fork this recipe", {:controller => "recipes", :action => "new", :parent => @recipe} %>

That seems to look fine to me, also the url reads fine when you get to the form, recipes/new?parent=112, but I still get that error, there has to be a way for that parameter to be passed as the parent of the newly created object.

A: 

If it works like acts_as_tree then I'll assume that you can do something like this:

@parent.children.create(attributes)

Which will create a new child object with the parent set, regardless of what the attributes say.

Ryan Bigg
how would I define what the `@parent` is?
Joseph Silvashy
would this be in my new or create action?
Joseph Silvashy
This would be in your create action, you define @parent like @parent = TreeNode.find(params[:parent_id])... it's up to you.
Ryan Bigg
I'll say you were right... well i guess I didn’t describe the issue in good enough detail, but you got me the closest, so cheers.
Joseph Silvashy
Brilliant then, happy to help
Ryan Bigg
A: 

According to the docs that you pasted you can do:

#...
@user = User.new(params[:user])
@user.parent_id = @parent_user.id
@user.save
#...

You can also include it in the params hash for the user -- your form submission would need to have params[:user][:parent_id]:

@user = User.create(params[:user])
bensie
So it's not actually part of a form, I have a `link_to` which will bring the user to a new form, the instance they create here will be a child of the element that had the `link_to` on it.
Joseph Silvashy
Like ultimatly id like to make it a nested route, something along the lines of `people/12/new`This would make a new person with 12 as the parent, ideas?
Joseph Silvashy