Rails 如何在 Engine 中使用主应用的 Concern 路由呢?[半解决]

dongli1985 · September 06, 2015 · 1696 hits

我在主应用中有如下路由:

concern :collectable do
  get '/collect' => 'collections#collect', as: :collect
  get '/uncollect' => 'collections#uncollect', as: :uncollect
end
...
resources :articles, concerns: :collectable

我在 engine 中有如下路由:

resources :experiments

我希望给experiments也加上collectable。如果直接加上concerns: :collectable显然无法认,我就把上面的concern :collectable do ... end拷贝到 engine 中,结果 Rails 去找我 engine 下的CollectionsController,但是又显然不存在。那如何优雅地解决这个问题呢?让程序更加 DRY 一些。

更新

自己采用了比较 ugly 的解决方法,还是拷贝concer :collectable do ... end到 engine,针对找不到<engine>::CollectionsController的错误,我在 engine 的 routes.rb 里添加如下一行:

<engine>::CollectionsController = CollectionsController

就没有问题,虽然不太 DRY,但是管用。如果你有任何建议欢迎提出。

No Reply at the moment.
You need to Sign in before reply, if you don't have an account, please Sign up first.