【问题标题】:inherited_resources gives wrong resource_path继承的资源给出了错误的资源路径
【发布时间】:2014-01-03 11:24:48
【问题描述】:

我正在使用 Rails 3.2.16 和inherited_resources 1.4.1。我需要一个快速的广告自定义管理员,所以我以这篇文章为例(它仍然适用于 rails 3.2):

http://iain.nl/backends-in-rails-3-1

这是我的路由文件的样子:

  namespace :backend do
    root to: 'conferences#index'
    resources :conferences do
      resources :talks
      resources :sponsors
    end
  end

我的Backend::ConferencesControllerBackend::SponsorsController 都继承自Backend::ResourceController,详见博文。

我发现的问题是,每当我转到Sponsors 索引页面时,我都会得到一个NoMethodError

NoMethodError: undefined method `backend_sponsor_path' for #<Backend::SponsorsController:0x007fa588113e08>

奇怪的是resource_path 方法试图找到backend_sponsor_path 而不是路由中声明的backend_conference_sponsor_path

有谁知道如何解决这个问题? inherited_resources不应该能找到正确的路径吗?

谢谢!

【问题讨论】:

    标签: ruby-on-rails-3.2 inherited-resources


    【解决方案1】:

    好的,通过在SponsorsController 中添加belongs_to :conference 解决了问题:

    class Backend::SponsorsController < Backend::ResourceController
      belongs_to :conference
    end
    

    现在路由已按预期生成! :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-23
      • 2016-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-18
      • 1970-01-01
      相关资源
      最近更新 更多