Rails 嵌套资源(多层) 类被视为模块

发布于 2024-10-24 09:56:27 字数 961 浏览 2 评论 0原文

这些是我的模型:

class Company < ActiveRecord::Base  
  has_many :products  
end

class Product < ActiveRecord::Base  
  belongs_to :company  
  has_many :prices  
end

class Price < ActiveRecord::Base  
  belongs_to :product  
end

我在路由中将它们定义为嵌套资源

resources :companies  
namespace :company do  
  scope ":company_id" do  
    resources :products do  
      resources :prices  
      resources :production_capabilities  
    end  
  end  
end

我想将控制器和视图放入与该结构匹配的目录中

app/controllers/companies_controller.rb  
app/controllers/company/products_controller.rb  
app/controllers/company/product  
app/controllers/company/product/prices_controller.rb

一旦我在公司内部创建产品目录并尝试调用

Company.find(1).products

NoMethodError: undefined method 'quoted_table_name' for Company::Product:Module

得到有人知道我做错了什么吗?

These are my models:

class Company < ActiveRecord::Base  
  has_many :products  
end

class Product < ActiveRecord::Base  
  belongs_to :company  
  has_many :prices  
end

class Price < ActiveRecord::Base  
  belongs_to :product  
end

I defined them in routes as nested resources

resources :companies  
namespace :company do  
  scope ":company_id" do  
    resources :products do  
      resources :prices  
      resources :production_capabilities  
    end  
  end  
end

I wanted to put controllers and views in catalogs matching that structure

app/controllers/companies_controller.rb  
app/controllers/company/products_controller.rb  
app/controllers/company/product  
app/controllers/company/product/prices_controller.rb

As soon as i create product directory inside company and i try to call

Company.find(1).products

i get

NoMethodError: undefined method 'quoted_table_name' for Company::Product:Module

Does anybody know what am i doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愿得七秒忆 2024-10-31 09:56:27

Rails 文档明确建议我们嵌套资源的深度不要超过 1 层:

http:// /guides.rubyonrails.org/routing.html#nested-resources

您将得到如下 URL:

/company/1/product/4/price/5

这不太漂亮。尽量避免它。

The Rails documentation explicitly recommends that we don't nest resources more than 1 level deep:

http://guides.rubyonrails.org/routing.html#nested-resources

You'll get URLs like this:

/company/1/product/4/price/5

That's not pretty. Try to avoid it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文