2011-12-21 18 views
4

このルートのパスヘルパーの作成方法は?Rails 3.1.3日付別カスタムルーティング

resources :news 

match 'news/:year/:month/:day' => 'news#show', 
    :constraints => { :year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/ }, 
    :as => 'newsdate' 

私はそれを多くの方法を試してみましたが、それはない作品:

link_to news.created_at.strftime '%d.%m.%Y ', newsdate_path(:year => '2011', :month => '11', :day => '11') 

私がGET http://localhost:3000/newsすることにより、このラインのためのアプリケーションエラーを取得:

ArgumentError in News#index 

    Showing /home/foxweb/work/dev/app/views/news/index.html.slim where line #6 raised: 

    wrong number of arguments (2 for 1) 

それ右の方法にする方法?

P.S. http://localhost:3000/news/2011/11/11は問題なく動作します。

答えて

5

ああ、それはよくある間違いです。あなたは中括弧でstrftime引数を取る必要があります。

link_to news.created_at.strftime('%d.%m.%Y'), newsdate_path(:year => '2011', :month => '11', :day => '11') 

これですべてです!

+0

つまり、経路ではなくリンク_内でエラーが発生しています。 –

+0

よろしくお願いします。しかし、 ルーティングエラー 'ルートは一致しません{:controller =>" news "、:action =>" show "、:year => 2004、:month => 9、:day => 15} ' – foxwwweb

+0

あなたの' rake routes'出力を見せてください。 –

関連する問題