Skip to content

Commit

Permalink
xml views
Browse files Browse the repository at this point in the history
  • Loading branch information
pjbelo committed Dec 16, 2018
1 parent dc8a2e8 commit 975f177
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/controllers/products_controller.rb
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions 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
16 changes: 16 additions & 0 deletions 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
6 changes: 6 additions & 0 deletions 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
1 change: 1 addition & 0 deletions 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

0 comments on commit 975f177

Please sign in to comment.