# polymorphic\_routes has\_one

**URL:** <https://discuss.rubyonrails.org/t/polymorphic-routes-has-one/31852>\
**Category:** rubyonrails-talk\
**Created:** [July 14, 2008, 7:35pm UTC](https://discuss.rubyonrails.org/t/polymorphic-routes-has-one/31852 "2008-07-14T19:35:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![julian4](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@julian4](https://discuss.rubyonrails.org/u/julian4)\
**Post date:** [July 14, 2008, 7:35pm UTC](https://discuss.rubyonrails.org/t/polymorphic-routes-has-one/31852/1 "2008-07-14T19:35:03Z")

</div>

So in my config/routes.rb I have :

map.resources :product\_lines, :has\_many =\> [:products, :documents], :has\_one =\> :image

Note: Images are polymorphic (imageable)

In my images controller I have:

&nbsp;&nbsp;def new &nbsp;&nbsp;&nbsp;&nbsp;@imageable = polymorphic\_parent(params) &nbsp;&nbsp;&nbsp;&nbsp;@image = Image.new &nbsp;&nbsp;...

The polymorphic\_parent function just looks through the params and finds one with \_id and finds the parent instance of the polymorphic relationship. So, in app/controllers/application.rb I have:

&nbsp;&nbsp;def polymorphic\_parent(params) &nbsp;&nbsp;&nbsp;&nbsp;for key, param in params &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if key.match(/\_id$/) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;klass = key[0...key.length - 3].classify &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return eval("#{klass}.find(param)") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

And in my app/views/images/new.html.erb I have:

\<% form\_for([@imageable, @image], :html =\> { :multipart =\> true }) do | f\> %\> ... etc....

But it throws and error because I guess polymorphic\_routes doesn't work with has\_one....

undefined method `product\_line\_images\_path' for #\<ActionView::Base: 0xb620f970\>

I think the path should be product\_line\_image\_path (image not images). Does anybody know If I can make this work? Am I doing something wrong?

Thanks

---

<div class="post-metadata">

**Author:** ![julian4](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@julian4](https://discuss.rubyonrails.org/u/julian4)\
**Post date:** [July 15, 2008, 1:14pm UTC](https://discuss.rubyonrails.org/t/polymorphic-routes-has-one/31852/2 "2008-07-15T13:14:35Z")

</div>

Well, it seems with edge rails there may have been an attempt to fix this. From actionpack/CHANGELOG:

Fix polymorphic\_url with singleton resources. #461 [Tammer Saleh]

I'm not sure what this patch intended to do, but from looking at the tests I guess I'm supposed to use a symbol to denote a singular resource. That does not seem to work with form\_for though because form\_for needs an object instance to be passed to the block. Note: This all works fine with a has\_many nested resource.

It's very strange to me that all of the routing is set up in routes.rb but then none of that information seems to be available to the url/ path helpers...
