自定網址
1
2
3
4
5
| class AddUrlToGroups < ActiveRecord::Migration
def change
add_column :groups, :url, :string
end
end
|
1
2
3
4
5
6
7
8
| #controller
def set_group
@group = Group.find_by_url(params[:id])
end
def update
@group = Group.find(params[:id])
end
|
1
2
3
4
5
6
7
8
9
10
11
12
| #model
class Group < ActiveRecord::Base
before_create :generate_url
protected
def generate_url
require 'digest/md5'
self.url = Digest::SHA1.hexdigest(self.title + Time.now.localtime.strftime("%m%d%H%M%S").to_s)
end
end
|
1
2
3
| #view
<%= link_to group.title, group_path(:controller => "group", :action => "show", :id => group.url) %>
|