Rails 嵌套资源(多层) 类被视为模块
这些是我的模型:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Rails 文档明确建议我们嵌套资源的深度不要超过 1 层:
http:// /guides.rubyonrails.org/routing.html#nested-resources
您将得到如下 URL:
这不太漂亮。尽量避免它。
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:
That's not pretty. Try to avoid it.