【发布时间】:2011-08-03 20:54:21
【问题描述】:
我有两个模型:Product 和 ProductType:
产品:
class Product < ActiveRecord::Base
default_scope :order => 'name'
has_many :sales
belongs_to :product_type, :class_name => "ProductType", :foreign_key => "type_id"
end
产品类型:
class ProductType < ActiveRecord::Base
default_scope :order => 'name'
has_many :products
end
在我的 routes.rb 中,我有:
resources :product_types do
resources :products
end
当我运行 rake 路由时,我看到这个 url 存在:
product_type_product GET /product_types/:product_type_id/products/:id(.:format) {:controller=>"products", :action=>"show"}
我遇到的问题是,当我导航到:http://localhost:3000/postering_locations/48/users 时,它会显示我拥有的具有 id 48 的发布位置的单个用户。问题是它还将所有其他用户显示为好吧。当我将 48 更改为另一个有效的数字时,也会发生同样的事情。我觉得它在部分工作,因为当我将它换成表中不存在的号码时,我收到路由错误。关于如何制作 /postering_locations/48/users 的任何想法实际上向我展示了 48 作为他们的 postering_location id 的用户?
另外,在我的 routes.rb 上,如果我仍然希望人们只能看到 /products/,我应该在哪里将资源 :products 添加到我的 routes.rb 中?
【问题讨论】:
标签: ruby-on-rails routes nested-routes