acts_as_tree -- how to create new child

I have:

steven_noble wrote:

    @project.parent_id = :parent
      respond_to do |format|

@project.parent = params[:parent]

Thanks Iian. I think the problem might be in my use of the following
in the projects/show view:

    <%= link_to 'Create Subproject', new_project_path, :parent =>
@project.id %>

I have <%= debug @project.id %> in that projects/show view, which
gives me:

    --- 21

However, in the subsequent projects/new view, where I have <%=
debug :parent %>, I get:

    --- :parent

not

    --- 21

It seems link_to is not passing the value from show action (showing
the parent project) to the new action (where I'm trying to create a
child project).

Any thoughts?

Thanks again,

Steven.

Thanks Iian. I think the problem might be in my use of the following
in the projects/show view:

    <%= link_to 'Create Subproject', new_project_path, :parent =>
@project.id %>

I have <%= debug @project.id %> in that projects/show view, which
gives me:

    --- 21

However, in the subsequent projects/new view, where I have <%=
debug :parent %>, I get:

    --- :parent

not

    --- 21

It seems link_to is not passing the value from show action (showing
the parent project) to the new action (where I'm trying to create a
child project).

Any thoughts?

Thanks again,

Steven.

Thanks. I'll try awesome_nested_set.

Iian seemed to be suggesting params[:parent] went into projects/new,
but I'm struggling to get a value to projects/new (via :parents) in
the first place, I assume for the reasons you've given.

Are you saying that params[:parent] goes into link_to to help me pass
the value to projects/new?

s.

Hi Marnen,

Thanks for sticking with me and sorry for not yet getting it.

Am I not doing what you say with the following in projects/show?

<%= link_to 'Create Subproject', new_project_path, :parent =>
@project.id %>

And should I not be able to retrieve the value in projects/new with
the following?

<%= debug :parent %>

s.

steven_noble wrote:

Hi Marnen,

Thanks for sticking with me and sorry for not yet getting it.

Am I not doing what you say with the following in projects/show?

<%= link_to 'Create Subproject', new_project_path, :parent =>
@project.id %>

And should I not be able to retrieve the value in projects/new with
the following?

<%= debug :parent %>

For the third time: params[:parent], not :parent ! :parent is just a
literal symbol. I already explained this; please reread.

Best,

I understand that's how you retrieve the value from the URL. My
question related to how to add the value to the URL first place. As I
suspected, the problem was with the syntax of my link_to. I've now
solved that problem. For anyone else facing this problem, a working
syntax is:

<%= link_to("Split Project Into Parts",
{:action=>"new_project_path", :parent => @project.id}) %>

Sorry:

<%= link_to("Split Project Into Parts",
{:controller=>"projects", :action=>"new", :parent => @project.id}) %>

Here's the complete code in case anyone else arrives here with the
same problem.

No doubt hardened Rubyists will be able to tidy up my syntax, but this
is passing all my specs right now so I'm happy. :slight_smile:

# app/models/project.rb

  acts_as_tree :order => "name"

# app/controllers/projects_controller.rb

  def new
    if params[:parent] == nil
      @parent = 0
    else
      @parent = Project.find(params[:parent]).id
    end
    ...
  end

# app/views/projects/show.html.erb

   <%= link_to("Create A Sub-Project",
{:controller=>"projects", :action=>"new", :parent => @project.id}) %>

# app/views/projects/new.html.erb

  <% form_for(@project) do |f| %>
     ...
     <%= f.hidden_field :parent_id, {:value => @parent} %>
     ...
  <% end %>