我設立自己的網站,使得在路線中,與#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
字面上只是想出來......傻了。謝謝,我會接受你的回答 –