Creating RESTful helpers on the fly

If I have a resource called ‘assets’ then I get for free a bunch of
helpers, like

assets_path
new_asset_path

and so forth, right?

I would like to be able to create a call to one of these on the fly when
I only find out the name of the resource at run time. For example I
would like to do this:

resource_name = “assets”
resource_path(resource_name, method => :new)

Anyone know the Rails/Rest/Routing incantation for that? Thanks!

Pito

On Nov 1, 4:40pm, Pito S. [email protected] wrote:

I would like to be able to create a call to one of these on the fly when
I only find out the name of the resource at run time. For example I
would like to do this:

resource_name = “assets”
resource_path(resource_name, method => :new)

Anyone know the Rails/Rest/Routing incantation for that? Thanks!

I believe you are looking for the “url_for” helper. Probably a number
of ways you can use it.

This one works, and uses the named “new_asset” route. Just make sure
resource_name is singular.

url_for((“new_”+resource_name).to_sym)

Or if the controllers have the standard REST naming, this should also
work:

url_for(:controller=>resource_name, :action=>:new)

I found this, which I think is going to work

ActionDispatch::Routing::PolymorphicRoutes

In: actionpack/lib/action_dispatch/routing/polymorphic_routes.rb:

"Polymorphic URL helpers are methods for smart resolution to a named
route call when given an Active Record model instance. They are to be
used in combination with ActionController::Resources.

These methods are useful when you want to generate correct URL or path
to a RESTful resource without having to know the exact type of the
record in question.

Nested resources and/or namespaces are also supported, as illustrated in
the example:

polymorphic_url([:admin, @article, @comment])
results in:

admin_article_comment_url(@article, @comment)
"

Thanks!