![](/img/trans.png)
[英]What's the best way to deal with English pluralisation in a rails view?
[英]What is the best way to deal with Vanity URL helpers in Rails 3?
我有一個我正在使用Rails 3的Web應用程序,我剛剛為應用程序中的現有資源實現了一些基本的虛榮URL路徑。 我要做的是不必在用戶的個人資料頁面上明確地建立可用資源的URL,例如我希望能夠在視圖中使用link_to構建URL,格式為:
typealoud.com/:user_id/:thread_id/:comment_id
而不是標准的嵌套資源助手給我的東西,例如:
typealoud.com/threads/:thread_id/comments/:comment_id
我應該自己做URL幫助,還是現有的寶石?
要做到這一點,我會把它放在我的路線的頂部:
match ':user_id/:thread_id/:id', :to => "comments#show"
我在這個例子中將comment_id
更改為id
因為它是“The Rails Way”,最后一個id參數簡稱為id
。 它還可以縮短代碼。
如果您希望有一個路由助手,請使用:as
選項:
match ':user_id/:thread_id/:id', :to => "comments#show", :as => "comment"
然后你可以使用comment_path
/ comment_url
來訪問路由,但你必須傳入三個參數,每個參數都是一個對象或一個對象的id。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.