views:

28

answers:

1

I'm using nested routes and I want to provide some sort of a shortcut method. (I'm using RoR 3.0)

The routes look like this.

resources :countries do
  resources :regions do
    resources :wineries
  end
end

To access a winery route I want to be able to define a function that removes the need to specify a country and region each time. Like:

def winery_path(winery)
  country_region_winery_path (winery.country, winery.region, winery)
end

Where should I do this? How can I get that to be available whereever url_for is available?

+1  A: 

I would put it into your app/controller/application_controller.rb

class ApplicationController < ActionController::Base
  helper_method :winery_path
  def winery_path(winery)
    country_region_winery_path (winery.country, winery.region, winery)
  end
end

Now it's available in every controller and view

jigfox