加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails 3.1.3按日期自定义路由

发布时间:2020-12-17 02:34:51 所属栏目:百科 来源:网络整理
导读:如何为这条路线制作路径助手? resources :newsmatch 'news/:year/:month/:day' = 'news#show',:constraints = { :year = /d{4}/,:month = /d{2}/,:day = /d{2}/ },:as = 'newsdate' 我尝试了很多方法,但它不起作用: link_to news.created_at.strftime '
如何为这条路线制作路径助手?

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)

如何正确的方式?

附: http://localhost:3000/news/2011/11/11工作正常.

解决方法

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

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

就这样!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读