Rails路线与日期 [英] Rails routes with date
问题描述
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
(注意 它会给你 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: 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 so, the correct way to do it would be for example: (notice the now, if you call 这篇关于Rails路线与日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!:as
现在,如果你调用 itineraries_date_path(11,12,1998)
行程/ 11/12/1998
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"
itineraries_path
)match "itineraries/:day/:month/:year" => "itineraries#index",
:constraints => { :year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/ },
:as => "itineraries_date"
:as
part)itineraries_date_path(11,12,1998)
it will give you itineraries/11/12/1998