How to dynamically called RESTful route helpers

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

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs