Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
67 lines (50 sloc) 916 Bytes

Custom routes in rails project

Installation

  • copy routes folder into your project
  • include include Routes::CustomControllerPaths in your ApplicationController

Usage

module Routes
  module CustomPaths
    def product_path(product)
      category_product_path(product.category_slug, product.slug)
    end
  end
end

In a controller

class ApplicationController
  # ...
  include Routes::CustomControllerPaths
  # ...

end
class ProductsController < ApplicationController
  def create
    # ...

    redirect_to product_path(@product)
  end
end
<%= link_to product.name, product_path(@product) %>

From Routes object

Routes.root_path
Routes.product_path(product)

As an instance variable

class Foo
  include Routes

  def routes
    {
      root: root_path
      product: product_path(product)
    }
  end
end