Skip to content

Instantly share code, notes, and snippets.

@trevorturk
Created October 5, 2010 19:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trevorturk/612127 to your computer and use it in GitHub Desktop.
Save trevorturk/612127 to your computer and use it in GitHub Desktop.
diff --git c/app/controllers/events_controller.rb w/app/controllers/events_controller.rb
index d7ce126..4e96ab9 100644
--- c/app/controllers/events_controller.rb
+++ w/app/controllers/events_controller.rb
@@ -17,4 +17,8 @@ class EventsController < TdsController
end
end
+ def show
+ @event = Event.find_by_slug! params[:id]
+ end
+
end
diff --git c/app/models/event.rb w/app/models/event.rb
index b972c15..7a56a0f 100644
--- c/app/models/event.rb
+++ w/app/models/event.rb
@@ -3,6 +3,21 @@ class Event < ActiveRecord::Base
attr_accessible :name
- validates_presence_of :name
+ validates_presence_of :name, :slug
+ validates_uniqueness_of :name, :slug
+
+ before_validation :generate_slug, :on => :create
+
+ def generate_slug
+ self.slug = to_param rescue nil
+ end
+
+ def to_s
+ name
+ end
+
+ def to_param
+ to_s.parameterize
+ end
end
diff --git c/app/views/events/index.html.erb w/app/views/events/index.html.erb
index 426ceea..45f2885 100644
--- c/app/views/events/index.html.erb
+++ w/app/views/events/index.html.erb
@@ -1,5 +1,5 @@
<% @events.each do |event| %>
- <p><%= event.inspect %></p>
+ <p><%= link_to event, event %></p>
<% end %>
<p><%= link_to 'new event', new_event_path %></p>
diff --git c/app/views/events/show.html.erb w/app/views/events/show.html.erb
new file mode 100644
index 0000000..8144001
--- /dev/null
+++ w/app/views/events/show.html.erb
@@ -0,0 +1 @@
+<%= @event.inspect %>
\ No newline at end of file
diff --git c/config/routes.rb w/config/routes.rb
index 4771522..c540f25 100644
--- c/config/routes.rb
+++ w/config/routes.rb
@@ -3,7 +3,7 @@ Showcase::Application.routes.draw do
root :to => "home#index"
resources :events,
- :only => [:index, :new, :create]
+ :only => [:index, :new, :create, :show]
devise_for :tds,
:path_names => {:sign_in => '/login'}
diff --git c/db/migrate/20101005185506_add_slug_to_events.rb w/db/migrate/20101005185506_add_slug_to_events.rb
new file mode 100644
index 0000000..1553a1b
--- /dev/null
+++ w/db/migrate/20101005185506_add_slug_to_events.rb
@@ -0,0 +1,9 @@
+class AddSlugToEvents < ActiveRecord::Migration
+ def self.up
+ add_column :events, :slug, :string
+ end
+
+ def self.down
+ remove_column :events, :slug
+ end
+end
diff --git c/db/schema.rb w/db/schema.rb
index 187da96..3d01c0e 100644
--- c/db/schema.rb
+++ w/db/schema.rb
@@ -10,13 +10,14 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20101005165426) do
+ActiveRecord::Schema.define(:version => 20101005185506) do
create_table "events", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "td_id"
+ t.string "slug"
end
create_table "tds", :force => true do |t|
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment