2010-03-12 21 views

回答

0

您可以使用一個稱爲SubDomain-Fu的寶石。請觀看此screen cast瞭解更多詳情。

+0

這是最好的方法嗎? 我想要的是提供像Facebook一樣的USER唯一URL。 facebook.com/newtongarcia facebook.com/username1 facebook.com/username2 – newx 2010-03-13 02:43:19

+0

在我的答案中鏈接的屏幕畫面與您的用例有相似的用例。觀看它的更多細節。 – 2010-03-13 03:00:30

4

我已經問這個問題上的Quora ..

http://www.quora.com/How-does-Quora-rewrite-their-urls

對於誰真正有興趣來實現類似Quora的/ Facebook的網址。 繼承人在Rails3中一個小竅門:

做一個表稱爲slugs

# slug | model_id | model 
# =========================== 
# simple data: 
# one | 2222 | post 
# two | 1111 | user 
# six | 5432 | comment 

現在,在routes.rb中加入這一行的底部:

match '/:id', :to => proc { |env| 
    id = env["action_dispatch.request.path_parameters"][:id] 
    model = Slug.find_by_slug(id).model 
    controller = [model.pluralize.camelize,"Controller"].join.constantize 
    controller.action("show").call(env) 
}, :as => :slugable 

所以,如果我們去/one在路線上,它翻譯爲:

id: one 
so from the slugs table, 
model: post 
and the route will point to "posts#show" 

現在在show在所有控制器中的動作

@something = Model.find_by_slug(params[:id])