Hi --
Hi --
David,
Thanks, my question is how to make this:
<% form_for "comment", :url => comments_path(@show,@episode) do |f|
%>
More dynamic, because it's a polymorphic model (comments)
Is there a way to make it so it can automatically pull the parents?
Something like <% form_for "comment", :url =>
comments_path(@parent1,@parent2) do |f| %>
And the controller will automatically figure out that @parent2 =
episode and @parent1 = show.
Anyway to do that?
OK, I see what you mean now. I wonder whether you could modify the
railsrevolution code so that parent_resources took an array... and
then you could do:
class CommentController < ApplicationController
parent_resources [:show, :episode], [:user, :post]
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