hey there, i'm trying to implement a drag and drop interface for a nested set in my first rails project. i'm new to rails so bear with me. my code is basically identical to this project: http://gist.github.com/128779. my problem is in the partial at this line:

<% for child in root.direct_children do %>

I'm getting a NoMethodError for direct_children which is an instance method of acts_as_nested_set, I believe. At the console if I try to create a new instance of my model, it is likewise unable to access the acts_as_nested_set instance methods, so I don't think the problem is in the partial but in the model.

Again, sorry if my terminology is wrong, I'm new to rails. Anyway, what am I doing wrong? I've got "acts_as_nested_set" in my model, just like the gist example above but my model does not appear to act as a nested set. How do I go about fixing this?

Thanks!

Here's the code for the model I am using (todo.rb):

class Todo < ActiveRecord::Base
  acts_as_nested_set
end

And here's the partial:

<% content_tag :li, :id => dom_id(root) do %>
    <%= content_tag :span, root.text %>
    <% content_tag :ul do %>
        <% for child in root.direct_children do %>
            <%= render :partial => "tree", :locals => {:root => child}%>
        <%end %>
    <%end unless root.direct_children.empty? %>
<%end%>

root is passed to the partial from the view like:

<%= render :partial => "tree", :locals => {:root => @root} %>

and @root is defined in the controller like:

@root = Todo.find_by_parent_id(nil)

Again, the code is mostly copied wholesale with very few modifications from the gist link above.

2

There are 2 best solutions below

0
On

A few things:

  • Have you checked that you installed the plugin properly? ./script/plugin install git://github.com/rails/acts_as_nested_set.git
  • Have you set up your table properly? Your model needs to have at least the following 3 columns by default (unless you want to override them): parent_id, lft, rgt. Without these acts_as_nested_set is going to have a hard time figuring out what's going on. I suggest you read the documentation at the top of this file because the readme doesn't say squat, nor does that gist for that matter.
  • If you've done the above, have you created a root element (not set the parent_id to anything) and then added at least one child to it?

    m = Model.new
    m.title = "My model's title"
    m.save!
    m2 = Model.new
    m2.title = "My child"
    m2.save!
    m.add_child(m2)
    

I just did a quick test using the above, and afterwards I was able to do things like m.root? and m.direct_children. Good luck.

0
On

What I make from your title is that you're using quite a bit more than acts_as_nested_set. Try removing some plugins and try again.