Skip to content

Instantly share code, notes, and snippets.

@senny
Created July 6, 2011 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save senny/1067232 to your computer and use it in GitHub Desktop.
Save senny/1067232 to your computer and use it in GitHub Desktop.
# Routes
# To prevent too deep nesting, we make the show, edit, update and destroy actions not nested under the category. We have a lot of models nested inside the product.
resources :category do
resources :products, :only => [:new, :create, :index]
end
resources :products, :only => [:show, :edit, :update, :destroy]
# ProductController
# We need to expose :category, :products and :product
class ProductController < ApplicationController
expose(:category) {
if params[:category_id].present?
Category.find(params[:category_id])
else
product.category
end
} # to support the half-nested half-unnested Routes setup above we have to fetch the category depending on the url-param
expose(:products) { category.products } # for the #index action
expose(:product)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment