2017-04-05 15 views
-2

我的代碼是這樣的:如何改變我訪問Rails的文章對象

article_path(Article.first) - 'http://localhost:3000/articles/1063' 
get 'articles/:id', to: 'articles#show', as: 'article' 

我想改變這樣的:

article_path(Article.first) - 'http://localhost:3000/articles/article-title-1063' 
get 'articles/:id', to: 'articles#show', as: 'article' , id: /some_regex/ 
+1

你可能想檢查'friendly-id' –

+0

而我認爲不是'http:// localhost:3000/articles/article-title-1063''你應該去http:// localhost: 3000 /用品/ 1063 /條,title'' –

回答

0

你可以做到這一點:

get 'articles/:article_title-:id', to: 'articles#show', as: 'article' 

並創建鏈接或路徑爲:

article_path(id: Article.first, article_title: "any-title-or-tag") 

article_path(id: Article.first, article_title: Article.first.title.gsub(' ','-') 

既然你喜歡,確保article_title PARAM不包含任何空格或其他特殊字符。

0

我想你應該設置

get 'articles/:article_title', to: 'articles#show', as: 'article' 

而在你的ArticlesController,你從params[:article_title]article_id