Rails 3.1.3按日期自定义路由

时间:2011-12-21 22:53:05

标签: ruby-on-rails ruby-on-rails-3 url

如何为此路线制作路径助手?

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工作正常。

1 个答案:

答案 0 :(得分:5)

哦,这是常见的错误。您需要在大括号中使用strftime个参数。

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

这就是全部!