Hi –
On Sat, 11 Aug 2007, [email protected] wrote:
<% form_for “comment”, :url => comments_path(@show,@episode) do |f|
episode and @parent1 = show.
etc. I haven’t tried it yet, but I think it could well work nicely.
Now I’ve tried it. Here’s a first iteration, anyway. First, add
these methods to the ones from the revolutiononrails blog post (in
application.rb):
def parent_ids(parent)
parent.map {|p| parent_id(p) }
end
def parent_types
self.class.parents.detect {|parent| parent_ids(parent) }
end
def parent_classes
parent_types && parent_types.map {|pt| parent_class(pt) }
end
def parent_nest
parent_classes && parent_classes.zip(parent_types).map {|pc, pt|
pc.find_by_id(parent_id(pt))
}
end
Now, in comments_controller.rb:
parent_resources [:show, :episode], [:user, :post]
def new
@parent_nest = parent_nest
@comment = Comment.new # still one of my least favorite common
# Rails idioms, but anyway
end
And then in the view – this part could use some refinement:
<% form_for @parent_nest << @comment do |f| %>
etc.
Since @parent_nest is already an array, you can’t just list it
alongside @comment inside an array – though of course you could
modify form_for so that that would be possible.
The kind of functional testing you’d want would be things like:
def test_parent_nesting
CommentsController.parent_resources([:show, :episode], [:user,
:post])
get(“new”, :user_id => 1, :post_id => 1)
assert_equal([:user, :post], @controller.send(:parent_types))
assert_equal([User, Post], @controller.send(:parent_classes))
assert_equal([User.find(1), Post.find(1)],
@controller.send(:parent_nest))
end
Hopefully that will get you started.
David
–