开发者

Rails nested routes not filtering

I have two models: Product and ProductType:

Product:

class Product < ActiveRecord::Base
    default_scope :order => 'name'

    has_many :sales

    belongs_to :product_type, :class_name => "ProductType", :foreign_key => "type_id"   
end

ProductType:

class ProductType < ActiveRecord::Base
    default_scope :order => 'name'

    has_many :products
end

In my routes.rb, I have:

resources :product_types do
    resources :products
end

When I run rake routes, I see that this url exists:

        product_type_product GET    /product_types/:product_type_id/products/:id(.:format)               {:controller=>"products", :action=>"show"}

The issue I'm having is that when I navigate to: http://localhost:3000/postering_locations/48/users, it will show me the single user I have that has the posteri开发者_StackOverflow中文版ng location with id 48. The issue is that it also displays all the other users as well.The same thing happens when I change the 48 to another number that is valid. I feel like it is partially working though because when I exchange it for a number that doesn't exist in the table, I receive a routing error. Any thoughts on how to make /postering_locations/48/users actually show me the users with 48 as their postering_location id?

Also, on my routes.rb, if I still want people to be able to see just /products/, where do I add the resources :products into my routes.rb?


I guess you should simply edit your controller's action:

@products = ProductType.find(params[:product_type_id]).products

As for your second question, simply add the mere:

resources :products

To your routes.rb. In routing the rule is first match first served. But since the nested and unnested url won't match each other, you're fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜