Add shortcut to nested route

I am using nested routes and I want to provide some kind of quick access method. (I am using RoR 3.0)

The routes are as follows.

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

In order to access the route of the winery, I want to be able to define a function that eliminates the need to specify a country and region each time. How:

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

Where should I do this? How can I get this if url_for is available?

+3
source share
1 answer

I would put it in 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 is available for each controller and view.

+1
source

Source: https://habr.com/ru/post/1744915/


All Articles