Rails Single resource as nested resource of two other resources

I have a business, catalog and product resource.

The business has a catalog and a range of products.

The catalog will always belong to the business.

The product may or may not be under the catalog.

The product table has catalog_id and business_id.

How do I form a route so that I can resolve a product without a directory and a product that belongs to the directory i.e. these urls:

  • business /: business_id / catalogs /: catalog_id / products /: id
  • business /: business_id / products /: id

I've already allowed the latter to use this:

resources :businesses do
    resources :catalogs

    resources :products do
      resources :images
      end
  end

      

how to change it to allow the first url?

I know I just missed something simple, feel free to close this and reference the duplicate if there is one.

Thank you so much!

+3


source to share


1 answer


Well, you were almost there:

resources :businesses do
    resources :catalogs do
      resources :products do
        resources :images
      end
    end

    resources :products do
      resources :images
    end
end

      



Just like for businesses / products, which you can also do for businesses / catalogs / products, there is no difference, just invest resources.

+7


source







All Articles