代码之家  ›  专栏  ›  技术社区  ›  sebastyuiop

有日期和很多

  •  4
  • sebastyuiop  · 技术社区  · 17 年前

    我有以下设置:

     map.resources :articles
     map.resources :users, :has_many => [:articles]
    

    我可以做/users/2/articles来获取给定用户的文章列表。

    我还有一个路线设置要做:

    map.connect 'articles/month/:month/:year', :controller => 'articles', :action => 'index'
    

    它可以找到一个月和一年的所有文章。

    我现在要做的是显示给定用户在给定月份和年份的文章。

    可能是类似于/users/2/articles/month/4/2009的URL

    有没有一个很好的方法?谢谢

    2 回复  |  直到 17 年前
        1
  •  2
  •   Val    17 年前
    map.resources :user do |user|
      user.connect 'articles/:month/:year', :controller => 'articles'
    end
    
        2
  •  1
  •   Damien MATHIEU    17 年前

    我不倾向于使用基于资源的路由,因此在我看来,以下内容应该有效

    map.connect 'users/articles/:user_id/:month/:year', :controller => 'users', :action => 'articles', :user_id => 0, :month => Time.now.strftime("%m"), :year => Time.now.strftime("%Y")
    

    这应允许以下URL /users/articles/2/11/2009 并提供当前月份和年份作为默认值,以备您使用 /users/articles/2 作为一个基本的URL,当您想要更具体的时候,可以提供一个月或一年作为附加值。

    它将路由到“用户”控制器和“项目”操作,其他值作为参数位于params[]哈希中。

    此外,您应该能够更改连接到的顺序:

    map.connect 'users/:user_id/articles/:month/:year'
    

    其余的保持不变以获得URL: /users/2/articles/11/2009