# Integrating polymorphic models into controllers views? how?

**URL:** <https://www.ruby-forum.com/t/integrating-polymorphic-models-into-controllers-views-how/111714>\
**Category:** Rails\
**Created:** [August 10, 2007, 12:55am UTC](https://www.ruby-forum.com/t/integrating-polymorphic-models-into-controllers-views-how/111714 "2007-08-10T00:55:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [August 10, 2007, 12:55am UTC](https://www.ruby-forum.com/t/integrating-polymorphic-models-into-controllers-views-how/111714/1 "2007-08-10T00:55:15Z")

</div>

I’m having trouble trying to get this to work.

How the heck do you get a polymorphic controller to work? Especially  
integrating it with a view?

Let’s say that we have shows, and episodes, then comments.

A show has many episodes, an episode has many comments.

the route is /shows/1/episodes/1/

Now to add a comment to that episode, how would I do it? Let’s say  
that the comment is polymorphic, how do I create a dynamic view for  
polymorphic assocations? This has the same question towards a  
controller. I have taken a look at  
[http://revolutiononrails.blogspot.com/2](http://revolutiononrails.blogspot.com/2)  
… llers.html but that still doesn’t answer my question with two  
parents?

I need something like

- form\_for([@parent1, @parent2, :comment]) do |f|  
= f.text\_area :comment  
= f.submit ‘Comment!’

Any help would be awesome, thanks!

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [August 10, 2007, 9:42pm UTC](https://www.ruby-forum.com/t/integrating-polymorphic-models-into-controllers-views-how/111714/2 "2007-08-10T21:42:28Z")

</div>

Anyone?

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [August 10, 2007, 9:57pm UTC](https://www.ruby-forum.com/t/integrating-polymorphic-models-into-controllers-views-how/111714/3 "2007-08-10T21:57:30Z")

</div>

Hi –

On Fri, 10 Aug 2007, [DanielFischer.com](http://DanielFischer.com) wrote:

> > the route is /shows/1/episodes/1/
> > 
> > - form\_for([@parent1, @parent2, :comment]) do |f|  
> > = f.text\_area :comment  
> > = f.submit ‘Comment!’
> > 
> > Any help would be awesome, thanks!

Try this in routes.rb:

map.resources :shows do |s|  
s.resources :episodes do |e|  
e.resources :comments  
end  
end

and then in the view:

\<% form\_for “comment”, :url =\> comments\_path(@show,@episode) do |f|  
%\>

assuming you’ve set @show and @episode in the controller.

David

–

- Books:  
RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242](http://www.awprofessional.com/title/0321509242))  
RUBY FOR RAILS ([Ruby for Rails](http://www.manning.com/black))
- Ruby/Rails training  
& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [August 11, 2007, 2:36pm UTC](https://www.ruby-forum.com/t/integrating-polymorphic-models-into-controllers-views-how/111714/4 "2007-08-11T14:36:36Z")

</div>

Hi –

On Fri, 10 Aug 2007, [DanielFischer.com](http://DanielFischer.com) wrote:

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

(This is also reminding me that I really must get back to work on the  
Inferred Routes plugin, which lets you skip the parent nesting for  
nested resource routes, and see how it shapes up with all the recent  
changes.)

David

–

- Books:  
RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242](http://www.awprofessional.com/title/0321509242))  
RUBY FOR RAILS ([Ruby for Rails](http://www.manning.com/black))
- Ruby/Rails training  
& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [August 10, 2007, 10:46pm UTC](https://www.ruby-forum.com/t/integrating-polymorphic-models-into-controllers-views-how/111714/5 "2007-08-10T22:46:35Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [August 11, 2007, 2:37pm UTC](https://www.ruby-forum.com/t/integrating-polymorphic-models-into-controllers-views-how/111714/6 "2007-08-11T14:37:37Z")

</div>

On Sat, 11 Aug 2007, removed\_email\_address@domain.invalid wrote:

> OK, I see what you mean now. I wonder whether you could modify the  
> railsrevolution code so that parent\_resources took an array… and

Whoops, I mean revolutiononrails. /me reaches for coffee…

David

–

- Books:  
RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242](http://www.awprofessional.com/title/0321509242))  
RUBY FOR RAILS ([Ruby for Rails](http://www.manning.com/black))
- Ruby/Rails training  
& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [August 12, 2007, 9:06pm UTC](https://www.ruby-forum.com/t/integrating-polymorphic-models-into-controllers-views-how/111714/7 "2007-08-12T21:06:03Z")

</div>

Daniel F. wrote:

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

In application\_helper.rb:

def polymorphic\_comments\_path(item)  
send("#{item.class.to\_s.underscore}\_comments\_path", item)  
end

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [August 12, 2007, 8:30pm UTC](https://www.ruby-forum.com/t/integrating-polymorphic-models-into-controllers-views-how/111714/8 "2007-08-12T20:30:20Z")

</div>

Hi –

On Sat, 11 Aug 2007, removed\_email\_address@domain.invalid 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

–

- Books:  
RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242](http://www.awprofessional.com/title/0321509242))  
RUBY FOR RAILS ([Ruby for Rails](http://www.manning.com/black))
- Ruby/Rails training  
& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))
