2010-03-12 18 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])