Is there a built-in method in Rails to construct urls from an array of models, given these conditions?
- Nesting will be 2-3 models deep
- Each node in the tree will have an unknown class
- You could have single table inheritance (
User
, andAdmin < User
, etc.)
How do I create urls from models like this:
[@user, @comment].to_url #=> '/users/10/comments/21'
[@admin_user, @task].to_url #=> '/admins/1/tasks/12'
I don't know the objects in the array necessarily either, could be like this (array of arrays, e.g. list of urls):
[[@user, @comment], [@task], [@admin, @task]].map(&:to_url)
Something like that. Is there anything in Rails built in to do this? Manually writing admin_task_url(@admin, @task)
isn't dynamic enough. I could do model.class.name.underscore...
and construct the url, but I'm wondering if there's another way.
Thanks!