如何在我的Rails應用程序上使用友好的URL? 所有我需要的是提供以下URL格式如何在mysite.com/[USERNAME]的Rails中使用友好的ID
mysite.com/company1 mysite.com/user123 mysite.com/user1234 mysite.com/newton.garcia mysite.com/company-name
任何人都可以幫忙嗎?
如何在我的Rails應用程序上使用友好的URL? 所有我需要的是提供以下URL格式如何在mysite.com/[USERNAME]的Rails中使用友好的ID
mysite.com/company1 mysite.com/user123 mysite.com/user1234 mysite.com/newton.garcia mysite.com/company-name
任何人都可以幫忙嗎?
您可以使用一個稱爲SubDomain-Fu的寶石。請觀看此screen cast瞭解更多詳情。
我已經問這個問題上的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])
這是最好的方法嗎? 我想要的是提供像Facebook一樣的USER唯一URL。 facebook.com/newtongarcia facebook.com/username1 facebook.com/username2 – newx 2010-03-13 02:43:19
在我的答案中鏈接的屏幕畫面與您的用例有相似的用例。觀看它的更多細節。 – 2010-03-13 03:00:30