Tidying up *_path methods for nested resources

Hi,

I’ve got this nested resource which is 3 deep, and I’m getting fed up writing something along the lines of:

child_path(@child.parent.grandparent, @child.parent, @child)

every time I want to link to one of those children. I’m wondering how to go about drying it up. One thing that had struck me was that I could override +child_path+, along the lines of:

def child_path_with_auto_parent(child)
  child_path_without_auto_parent(child.parent.grandparent, child.parent, child)
end
alias_method_chain :child_path, :auto_parent

Of course, I’d have to do the same for all the named routes, which is a little laborious, but at least it tidies up the rest of my code. The question is: where would I put that chunk of code? Where are named route methods pushed in to? There appears to be a section_path in ActionController::Base, but putting this code in there didn’t appear to do the desired thing. Maybe it’s also defined in ActionView somewhere?

Or is there an even better way of tidying up these _path methods so they only take a single parameter?

Trying to follow through the routing code gives me a headache…

Cheers,

Graeme

Hi Mathie,

have you tried acts_as_resource plugin? Seems like what you need. I've used it in a project and it worked well.

-- bobeš

Hi,

I had, yes, but I missed the fact that it defines the helper methods in a better way. Oops! That teaches me for assuming there would be no real code in acts_as_resource/init.rb. :slight_smile:

Cheers,

Graeme