Forum: Ruby on Rails Tidying up *_path methods for nested resources

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Graeme Mathieson (Guest)
on 2007-02-17 17:58
(Received via mailing list)
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
--
Graeme Mathieson

Rubaidh Ltd: Scottish for Ruby on Rails <http://www.rubaidh.com/>

Personal blog: <http://woss.name/>

Rubaidh Ltd is a company registered in Scotland with registration number
SC297029.  The registered address is:

Rubaidh Ltd
12d Monktonhall Terrace
Musselburgh
East Lothian.  EH21 6ER
United Kingdom
bobes (Guest)
on 2007-02-17 18:20
(Received via mailing list)
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Ň°
Graeme Mathieson (Guest)
on 2007-02-17 18:32
(Received via mailing list)
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. :-)

Cheers,

Graeme

On 17/02/07, bobes <removed_email_address@domain.invalid> wrote:
> > Hi,
> > +child_path+, along the lines of:
> > question is:  where would I put that chunk of code?  Where are named
> >
> > Rubaidh Ltd is a company registered in Scotland with registration number
>
--
Graeme Mathieson

Rubaidh Ltd: Scottish for Ruby on Rails <http://www.rubaidh.com/>

Personal blog: <http://woss.name/>

Rubaidh Ltd is a company registered in Scotland with registration number
SC297029.  The registered address is:

Rubaidh Ltd
12d Monktonhall Terrace
Musselburgh
East Lothian.  EH21 6ER
United Kingdom
This topic is locked and can not be replied to.