Acts_as_tree -- how to create new child

I have:

In the Projects model…

acts_as_tree :order => “name”

In the Projects/show view…

<%= link_to ‘Create Subproject’, new_project_path, { :parent =>
@project.id } %>

In the Projects controller…
def new
@project = Project.new
@project.parent_id = :parent
respond_to do |format|
format.html
end
end

Yet in script/console, I can see each new project has a parent_id of
nil. How do I pass the value of :parent to a new project when I’m
creating it as a subproject?

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.

steven_noble wrote:
[…]

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

--- :parent

not

--- 21

Well, of course. :parent is a literal symbol, not a variable name. It
will never equal anything other than :parent. I think you’re looking for
params[:parent], as Ilan already said.

BTW, you probably want aweome_nested_set, not acts_as_tree.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

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.

On Aug 7, 1:34 am, Marnen Laibow-Koser <rails-mailing-l…@andreas-

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.

On Aug 7, 7:03 am, Marnen Laibow-Koser <rails-mailing-l…@andreas-

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,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

s.

On Aug 7, 7:03�am, Marnen Laibow-Koser <rails-mailing-l…@andreas-

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}) %>

On Aug 7, 7:21 am, Marnen Laibow-Koser <rails-mailing-l…@andreas-

steven_noble wrote:

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?

Ilan suggested using link_to ‘whatever’, :parent => some_parent_value .
If I remember my link_to syntax correctly, any “extra” parameters in the
link_to will be accessible in params. So yeah, when you click the link
generated by the link_to statement, some_parent_value will be passed to
the ‘whatever’ action as params[:parent].

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

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 %>

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs