# Use url\_for with singleton resources?

**URL:** <https://www.ruby-forum.com/t/use-url-for-with-singleton-resources/111851>\
**Category:** Rails\
**Created:** [August 12, 2007, 10:20am UTC](https://www.ruby-forum.com/t/use-url-for-with-singleton-resources/111851 "2007-08-12T10:20:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![malesca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@malesca](https://www.ruby-forum.com/u/malesca)\
**Post date:** [August 12, 2007, 10:20am UTC](https://www.ruby-forum.com/t/use-url-for-with-singleton-resources/111851/1 "2007-08-12T10:20:18Z")

</div>

Has anyone had any success with url\_for and singleton resources?

I have a singleton forum nested under both groups and places:  
map.resources :groups do |group|  
group.resource :forum  
end  
map.resources :places do |group|  
group.resource :forum  
end

So I have paths like /groups/123/forum and /places/456/forum.

In the forum controller, I do what essentially amounts to  
@owner = Group.find\_by\_id(params[:group\_id]) ||  
Place.find\_by\_id(params[:place\_id])  
so that @owner points to the group or place that has the forum in  
question.

In the forum views, I want a link that is polymorphic in relation to  
@owner: it should resolve into group\_forum\_path(@owner.id) or  
place\_forum\_path(@owner.id) depending on the value of @owner.

I’ve tried url\_for([@owner, @owner.forum]), but I get this error: “You  
have a nil object when you didn’t expect it! The error occurred while  
evaluating nil.to\_sym”. Debugging it a bit, I find that the method  
name is correctly resolved into group\_forum\_path, but this method is  
then sent _both_ arguments, where it should only be sent the @owner.

Is this a bug? Am I using url\_for incorrectly? Anyone have a solution?

For now, I wrote my own url helper to work around it. I might try to  
fix url\_for if I find the time, but first I want to find out if it is  
in fact broken.

---

<div class="post-metadata">

**Author:** ![malesca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@malesca](https://www.ruby-forum.com/u/malesca)\
**Post date:** [August 12, 2007, 10:59am UTC](https://www.ruby-forum.com/t/use-url-for-with-singleton-resources/111851/2 "2007-08-12T10:59:20Z")

</div>

On Aug 12, 1:19 am, Henrik N [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Has anyone had any success with url\_for and singleton resources?

So if I patch like [http://pastie.textmate.org/86956](http://pastie.textmate.org/86956), I can do e.g.  
polymorphic\_url([@parent, @forum], :singleton =\> true, :routing\_type  
=\> :path)  
In practice I’d wrap it.

Is there some way to figure out if the generated route is a singleton,  
though, so you don’t have to specify it manually like this? I found  
[http://dev.rubyonrails.org/ticket/8830](http://dev.rubyonrails.org/ticket/8830) /  
[http://dev.rubyonrails.org/attachment/ticket/8830/polymorphic\_routes\_for\_singleton\_resources.diff](http://dev.rubyonrails.org/attachment/ticket/8830/polymorphic_routes_for_singleton_resources.diff)  
which makes use of “plural\_route”, but a grep in edge doesn’t turn up  
anything of the sort. Google only finds the diff in question.
