2013-07-08 46 views
1

我設立自己的網站,使得在路線中,與#index匹配不起作用...爲什麼?

www.website.com/articles/2013/07將列出所有文章從2013年七月和 www.website.com/articles/2013將列出2013 www.website.com/articles/2013/07/title-of-article所有文章將只列出具體的文章

但是,我能夠只得到上述3箇中的第一個和最後一個工作。輸入網址 www.website.com/articles/2013無法正常工作。具體來說,我得到一個noMethodError in Articles#show,這對我來說沒有意義,因爲我已經匹配到Articles#index的路線。

有人能解釋一下這裏發生了什麼嗎?我不明白我犯了什麼錯誤。

路線:

match "/articles/:year", :to => "articles#index", 
:constraints => { :year => /\d{4}/ }, :as=> :posts_year 

match "/articles/:year/:month", :to => "articles#index", 
:constraints => { :year => /\d{4}/, :month => /\d{1,2}/ }, :as => :posts_month 

match "/articles/:year/:month/:slug", :to => "articles#show", 
:constraints => { :year => /\d{4}/, :month => /\d{1,2}/, :slug => /[a-z0-9\-]+/ }, :as => :posts_date 

,並在我的模型,我有:

def year 
    created_at.year 
end 

def month 
    created_at.strftime("%m") 
end 

回答

1

你有你的routes文件resources :articles?如果你這樣做,那就解釋錯誤。將resources :articles移到您在問題中提到的路線下面,一切都應該正常工作。另外,您可以將其更改爲:

resources :articles, :except => :show 
+1

字面上只是想出來......傻了。謝謝,我會接受你的回答 –

0

猜測,如果您已經定義的文章作爲一種資源,那麼默認的顯示/:ID路由優先級高於您:posts_year路線。也許試試resources :articles, :except => [:show]

相關問題