On Mar 29, 1:14 pm, Ben Alpert <[email protected]> wrote:
> (Copied from my stagnant SO post:http://stackoverflow.com/q/5462652/49485.
> Feel free to answer there and get reputation if you like.)
>
> With nested resource routes in Rails 3, such as the following:
>
>     resources :magazines do
>       resources :ads
>     end
>
> helpers such as `magazine_ad_path` are defined, to which I have to
> pass both a magazine and the ad, which is inconvenient if I just have
> a reference to the ad:
>
>     magazine_ad_path(@ad.magazine, @ad)
>
> Is there a nice way to set up an `ad_path` helper that takes the `@ad`
> and returns the appropriate address including the magazine ID? (This
> would also then allow the use of `link_to @ad`, `redirect_to @ad`,
> etc., which automatically call the `ad_path` helper corresponding to
> the model class.)

I think there are enough valid reasons to have both magazine_ad_path
and ad_path that you couldn't easily form a convention to handle one
or the other.  For instance, why in your example do you not also do

    resources :ads

You only need /ads/# to get at an ad, and with this you could do your
'link_to @ad'.  If there is a reason that the /magazines/#/ is
important (say to render a different view if its under a magazine vs
not) then when you have links you need to specify which particular
path you want anyway.  If that's not important you can do (forgive my
syntax, I'm on rails 2.3.x)

    resources :magazines do
      resources :ads, :only => [:index, :new, :create]
    end
    resources :ads, :except => [:index, :new, :create]

Then you could do your 'link_to @ad', or ad_path as expected, but
you'd also be forced to do magazine_ads_path, which is as expected.

Related to this, with you current mappings, you can do:

    link_to [@magazine, @ad]

I'm guessing that doesn't address your main concern about having to
include @magazine when its unnecessary, which is why I listed the
other options/explanations first.

\Peter

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to