Commit e68d9bd4

mo khan <mo@mokhan.ca>
2013-06-21 18:49:39
clean up routes to the categories controller
1 parent f3d554b
Changed files (4)
app
config
app/controllers/categories_controller.rb
@@ -1,6 +1,7 @@
 class CategoriesController < ApplicationController
   def show
-    @category = Category.find_by_slug(params[:id])
+    @category = Category.find_by_slug(params[:slug])
+    @categories = Category.all
     @creations = @category.creations.includes(:user).page(params[:page]).per(12)
   end
 end
app/models/category.rb
@@ -1,4 +1,8 @@
 class Category < ActiveRecord::Base
   has_and_belongs_to_many :creations, :join_table => 'creations_categories'
   attr_accessible :name, :slug
+
+  def to_param
+    slug
+  end
 end
app/views/categories/show.html.erb
@@ -1,13 +1,12 @@
 <% provide(:title, "#{@category.name} Cakes") -%>
 <div class="row">
   <div class="span3">
-    <h1>Categories</h1>
     <ul class="well nav nav-pills nav-stacked">
-      <% Category.all.each do |category| %>
+      <% @categories.each do |category| %>
         <% if(category == @category) -%>
-          <li class="active"><a href="/categories/<%= category.slug %>"><%= category.name %></a></li>
+          <li class="active"><%= link_to category.name, category_path(category) %></li>
         <% else -%>
-          <li><a href="/categories/<%= category.slug %>"><%= category.name %></a></li>
+          <li><%= link_to category.name, category_path(category) %></li>
         <% end -%>
       <% end %>
     </ul>
config/routes.rb
@@ -1,10 +1,4 @@
 Cake::Application.routes.draw do
-  ActiveAdmin.routes(self)
-
-  devise_for :admin_users, ActiveAdmin::Devise.config
-
-  root :to => "creations#index"
-
   # /home
   match "about_us" => "home#about_us"
   match "why_cakeside" => "home#why_cakeside"
@@ -28,9 +22,8 @@ Cake::Application.routes.draw do
   match 'favorites' => 'profiles#favorites', :as => 'profiles_favorites', :method => 'GET'
 
   # /categories
-  resources :categories, :only => [:show] do
-    get 'page/:page', :action => :show, :on => :collection
-  end
+  get 'categories/:slug' => "categories#show", :as => :category
+  get 'categories/:slug/page/:page' => "categories#show"
 
   # /tags
   resources :tags, :only => [:show] do
@@ -49,4 +42,11 @@ Cake::Application.routes.draw do
   match "/sitemap.xml", :to => "sitemap#index", :defaults => {:format => :xml}
 
   match 'settings/change_password' => 'settings#change_password', :as => 'settings_change_password', :method => 'POST'
+
+  ActiveAdmin.routes(self)
+
+  devise_for :admin_users, ActiveAdmin::Devise.config
+
+  root :to => "creations#index"
+
 end