Topic: route helpers for nested catalog categories and items

I develop online shop and I need organize non standard routing scheme for catalog categories and catalog items.

For example,
index page of the category should have path:

and show page of the catalog item should have path:

I have records in routes.rb:

get '/*catalog_path' => 'catalog_categories#show', constraints:, as: :catalog_category
get '/*catalog_path' => 'catalog_items#show', as: :catalog_item

and all works fine, but I need use pretty named helpers, like


for generating proper nested urls.

For solving this problem I define helper methods in application controller

helper_method :catalog_item_path

def catalog_item_path(catalog_item)
  catalog_path = (catalog_item.categories_path_to_root + [catalog_item.slug]).join('/')
  super(catalog_path: catalog_path)

and I can access to this helpers in all views and controllers.


Can I define / patch / override this helpers in Rails.application.routes.url_helpers ?

I need it for access to this helpers in several service classes and modules that do not have direct access to controller and view contexts. For example I have exporter class

class CatalogXmlExporter
  include Rails.application.routes.url_helpers
  def export(xml)
    # some code
    xml.url catalog_item_path(current_catalog_item)
    # some code

and catalog_item_path not return right value here.

Sorry for my basic english skills and many thanks for your advice:)

Last edited by rainrb (2013-03-07 10:49:48)