Rails路线与日期 [英] Rails routes with date

查看:166
本文介绍了Rails路线与日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是关于在rails应用程序的路由中按日期使用过滤器,当时我已经准备好了与routes.rb文件中的日期模式匹配的规则,它看起来像这样: p>

  match行程/:day /:month /:year=> 行程#index,
:constraints => {:year => / \d {4} /,:month => / \d {2} /,:day => / \d {2} /}
match行程/ new /:day /:month /:year=> 行程#新,
:constraints => {:year => / \d {4} /,:month => / \d {2} /,:day => / $ \\\\\\\\\\\\\\\\\\\\\\\\

资源:行程
匹配' 行程#索引

匹配例如/ itineraries / 01/01/2011,问题出现当我从资源生成路由时,例如,行程路径(:year => 2011,:month => 1,:day => 1)生成:



/行程?day = 1& month = 1& year2011



而不是



/行程/ 01/01 / 2011



有没有办法把匹配规则放在资源映射中?

解决方案 itineraries_path )



所以,正确的方法就是:

  match行程/:day /:month /:year=> 行程#index,
:constraints => {:year => / \d {4} /,:month => / \d {2} /,:day => / \d {2} /},
:as => itineraries_date

(注意:as 现在,如果你调用 itineraries_date_path(11,12,1998)

它会给你行程/ 11/12/1998


My question it's about use a filter by date in the route of a rails application, at the moment I all ready have the rule that match the pattern of the date in the routes.rb file, it's looks like this:

match "itineraries/:day/:month/:year" => "itineraries#index", 
        :constraints => { :year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/ }
match "itineraries/new/:day/:month/:year" => "itineraries#new", 
        :constraints => { :year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/ }

resources :itineraries
match '/:controller(/:action(/:id))'
root :to => "itineraries#index"

That match for example /itineraries/01/01/2011, the problem comes when I generate a route from the resource, for example, itineraries_path(:year=>2011,:month=>1,:day=>1) generate:

/itineraries?day=1&month=1&year2011

instead of

/itineraries/01/01/2011

Is there a way to put the match rule inside the resource mapping?

解决方案

well, short answer is your route does exists, but it's not named yet (check rake routes to convince yourself) and that's why you can't call it just like that (using itineraries_path)

so, the correct way to do it would be for example:

match "itineraries/:day/:month/:year" => "itineraries#index", 
    :constraints => { :year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/ },
    :as => "itineraries_date"

(notice the :as part)

now, if you call itineraries_date_path(11,12,1998) it will give you itineraries/11/12/1998

这篇关于Rails路线与日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆