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.
A few things:
./script/plugin install git://github.com/rails/acts_as_nested_set.git
parent_id
,lft
,rgt
. Without theseacts_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?I just did a quick test using the above, and afterwards I was able to do things like
m.root?
andm.direct_children
. Good luck.