如何在Rails应用程序中使用友好的URL?所有我需要的是使以下网址格式可用
mysite.com/company1 mysite.com/user123 mysite.com/user1234 mysite.com/newton.garcia mysite.com/company-name
有人能帮上忙吗?
发布于 2011-03-27 01:03:13
我已经在Quora上问过这个问题了..
http://www.quora.com/How-does-Quora-rewrite-their-urls
对于那些真正有兴趣实现Quora/Facebook URL的人。以下是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])发布于 2010-03-12 11:36:25
为此,您可以使用一个名为SubDomain-Fu的gem。有关更多详细信息,请观看此screen cast。
https://stackoverflow.com/questions/2430265
复制相似问题