diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index d3cca49..4bece07 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -12,6 +12,10 @@ def index def show end + def products_by_category + @categories = Category.all + end + # GET /products/new def new @product = Product.new diff --git a/app/views/products/index.xml.builder b/app/views/products/index.xml.builder new file mode 100644 index 0000000..c682a52 --- /dev/null +++ b/app/views/products/index.xml.builder @@ -0,0 +1,10 @@ +xml.instruct! +xml.products do + @products.each do |product| + xml.product do + xml.name product.name + xml.quantity product.quantity + xml.category product.category.name + end + end +end diff --git a/app/views/products/products_by_category.xml.builder b/app/views/products/products_by_category.xml.builder new file mode 100644 index 0000000..6f2a76e --- /dev/null +++ b/app/views/products/products_by_category.xml.builder @@ -0,0 +1,16 @@ +# app/views/products/products_by_category.xml.builder + +xml.instruct! +xml.products_by_category do + @categories.each do |category| + xml.category do + xml.name category.name + category.products.each do |product| + xml.product do + xml.name product.name + xml.quantity product.quantity + end + end + end + end +end diff --git a/app/views/products/show.xml.builder b/app/views/products/show.xml.builder new file mode 100644 index 0000000..52c00cd --- /dev/null +++ b/app/views/products/show.xml.builder @@ -0,0 +1,6 @@ +xml.instruct! +xml.product do + xml.name @product.name + xml.quantity(@product.quantity, type: 'integer') + xml.category @product.category.name +end diff --git a/config/routes.rb b/config/routes.rb index 9d1eabc..d28ce27 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,7 @@ Rails.application.routes.draw do root "products#index" resources :products + get '/products_by_category', to: 'products#products_by_category' resources :categories # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end