# How to dynamically called RESTful route helpers

**URL:** <https://www.ruby-forum.com/t/how-to-dynamically-called-restful-route-helpers/142753>\
**Category:** Rails\
**Created:** [July 11, 2008, 3:03am UTC](https://www.ruby-forum.com/t/how-to-dynamically-called-restful-route-helpers/142753 "2008-07-11T03:03:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 11, 2008, 3:03am UTC](https://www.ruby-forum.com/t/how-to-dynamically-called-restful-route-helpers/142753/1 "2008-07-11T03:03:50Z")

</div>

All,

I’m writing a generic application helper that will take an object and  
then attempt to generate the name of the appropriate RESTful route  
helper for POST and PUT.

Here it is so far:

def get\_form\_url(object)  
tableized\_name = object.class.name.tableize  
update\_helper = “#{tableized\_name}\_url”

#This should return the correct URL based on whether the object is new  
or not.  
#But what should be in ???  
???.send(‘update\_helper’, object)  
end

So, as an example, if I have an object of type “ServiceOption”, then the  
update\_helper will be “service\_options\_url”, which assuming that there  
is a  
“map.resources :service\_options” line in my routes.rb, will be a valid  
URL helper for this object.

The trick is I need to dynamically call the URL helper and I don’t know  
which object to send the message to.

What should ??? be above.

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 11, 2008, 3:04am UTC](https://www.ruby-forum.com/t/how-to-dynamically-called-restful-route-helpers/142753/2 "2008-07-11T03:04:44Z")

</div>

Correction:

> def get\_form\_url(object)  
> tableized\_name = object.class.name.tableize  
> update\_helper = “#{tableized\_name}\_url”
> 
> #This should return the correct URL based on whether the object is new  
> or not.  
> #But what should be in ???  
> ???.send(update\_helper, object)  
> end

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 11, 2008, 7:30am UTC](https://www.ruby-forum.com/t/how-to-dynamically-called-restful-route-helpers/142753/3 "2008-07-11T07:30:59Z")

</div>

ANSWER:

This is what the method should look like:

def get\_update\_url(object)  
camel\_case\_object\_name = object.class.name.underscore  
eval("#{camel\_case\_object\_name}\_url(object)")  
end

My problem was the camel case version of the class name was plural  
(tableize vs. underscore inflection).

This seems to work fine.

Thanks,  
Wes
