Skip to content

Commit

Permalink
Add slug to topics and quizzes for SEO friendly links
Browse files Browse the repository at this point in the history
  • Loading branch information
MrPowers committed Feb 1, 2014
1 parent a7cec67 commit e11e45e
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 14 deletions.
6 changes: 1 addition & 5 deletions app/controllers/families_controller.rb
Expand Up @@ -24,11 +24,7 @@ def topic_order
"CSS",
"jQuery",
"Git/Github",
"HTML / CSS",
"Unix / Bash",
"JavaScript",
"CoffeeScript",
"RSpec"
"Unix / Bash"
]
end
end
4 changes: 2 additions & 2 deletions app/controllers/quizzes_controller.rb
@@ -1,8 +1,8 @@
class QuizzesController < ApplicationController
def show
@topic = Topic.find(params[:topic_id])
@topic = Topic.where(slug: params[:topic_id]).first
authorize! :show, @topic
@quiz = Quiz.find(params[:id])
@quiz = Quiz.where(slug: params[:id]).first
authorize! :show, @quiz
if current_user
authorize! :create, Exam
Expand Down
4 changes: 4 additions & 0 deletions app/models/quiz.rb
Expand Up @@ -5,4 +5,8 @@ class Quiz < ActiveRecord::Base
has_many :exams
belongs_to :user
belongs_to :topic

def to_param
"#{slug}".parameterize
end
end
4 changes: 4 additions & 0 deletions app/models/topic.rb
Expand Up @@ -3,4 +3,8 @@ class Topic < ActiveRecord::Base
has_many :quizzes
belongs_to :family
belongs_to :user

def to_param
"#{slug}".parameterize
end
end
2 changes: 1 addition & 1 deletion app/views/families/show.html.haml
Expand Up @@ -7,6 +7,6 @@
.thumbnail
%h3= topic.name
- topic.quizzes.each do |quiz|
%span.quiz{:data => {:quiz_id => quiz.id}}= link_to quiz.body, topic_quiz_path(:topic_id => topic, :id => quiz)
%span.quiz{:data => {:quiz_id => quiz.id}}= link_to quiz.body, topic_quiz_path(:topic_id => topic.slug, :id => quiz.slug)
%br/
.description= quiz.description
10 changes: 5 additions & 5 deletions config/routes.rb
@@ -1,4 +1,6 @@
Compoundblingapplication::Application.routes.draw do
root :to => 'families#show', :id => 1

match 'stats' => 'user_statistics#index'

get "static_pages/about"
Expand All @@ -14,10 +16,6 @@

resources :families, :only => [:show]

resources :topics, :only => [] do
resources :quizzes, :only => [:show]
end

resources :quizzes, :only => [] do
resources :questions
end
Expand All @@ -28,5 +26,7 @@

devise_for :users

root :to => 'families#show', :id => 1
resources :topics, :path => "", :only => [] do
resources :quizzes, :path => "", :only => [:show]
end
end
6 changes: 6 additions & 0 deletions db/migrate/20140131232530_add_slug_to_topics.rb
@@ -0,0 +1,6 @@
class AddSlugToTopics < ActiveRecord::Migration
def change
add_column :topics, :slug, :string
add_index :topics, :slug
end
end
6 changes: 6 additions & 0 deletions db/migrate/20140131233558_add_slug_to_quizzes.rb
@@ -0,0 +1,6 @@
class AddSlugToQuizzes < ActiveRecord::Migration
def change
add_column :quizzes, :slug, :string
add_index :quizzes, :slug
end
end
8 changes: 7 additions & 1 deletion db/schema.rb
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20131230022808) do
ActiveRecord::Schema.define(:version => 20140131233558) do

create_table "answers", :force => true do |t|
t.integer "exam_id"
Expand Down Expand Up @@ -63,16 +63,22 @@
t.integer "user_id"
t.text "description"
t.integer "topic_id"
t.string "slug"
end

add_index "quizzes", ["slug"], :name => "index_quizzes_on_slug"

create_table "topics", :force => true do |t|
t.text "name"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "family_id"
t.string "slug"
end

add_index "topics", ["slug"], :name => "index_topics_on_slug"

create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
Expand Down

0 comments on commit e11e45e

Please sign in to comment.