Ruby on rails Rails路由问题-数据路由
我想允许几个路由成为Ruby on rails Rails路由问题-数据路由,ruby-on-rails,ruby-on-rails-4,routes,Ruby On Rails,Ruby On Rails 4,Routes,我想允许几个路由成为:datable,这意味着它们应该可以按日期过滤。以下是我的路线: # The datable concern concern :datable do member do get 'dated/:start_date/(:end_date)', as: 'dated', constraints: {start_date: /\d{4}-\d{2}-\d{2}/, end_date: /\d{4}-\d{2}-\d{2}/} end end # The rou
:datable
,这意味着它们应该可以按日期过滤。以下是我的路线:
# The datable concern
concern :datable do
member do
get 'dated/:start_date/(:end_date)', as: 'dated', constraints: {start_date: /\d{4}-\d{2}-\d{2}/, end_date: /\d{4}-\d{2}-\d{2}/}
end
end
# The route in which it's added
resources :applications, concerns: :datable
理想情况下,这将产生:
/applications/1/dated/:start_date/(:end_date)
但是,我遇到了一个路由错误:
ArgumentError: Missing :action key on routes definition, please check your routes.
很明显,我缺少了一个
:action
键,但为什么这对于一个问题来说是必要的呢?它不应该继承调用它的路由吗?Matt Brictson提醒我,该问题不知道在资源中路由到哪个操作。这显然是有道理的,不过,我仔细看了一下
这种担心完全没有问题。为了解决我遇到的问题,我需要将简单的resources:applications
替换为以下内容:
# Leave out the index resource, which is what I am "concerning"
resources :applications, except: [:index]
# Manually create the index route; adding the concern.
get 'applications', to: 'applications#index', as: 'applications', concerns: :datable
工作起来很有魅力。谢谢你的提示,马特。我希望这能让其他人了解这个问题。您所说的“继承路由”是什么意思?
resources
路由中有许多操作(索引、显示、新建等),Rails怎么知道它应该使用哪种操作?啊,我明白你的意思。这很有道理。我会用我如何解决这个问题来回答自己。谢谢,Matt!