From 4e4f0c32a275b1e2d397751e5cd34a6a0bc28cb9 Mon Sep 17 00:00:00 2001 From: Benjamin Cousaert Date: Fri, 5 Dec 2014 07:24:27 +0100 Subject: [PATCH] Add avatar to product edit form and use recources in routes --- app/controllers/orders_controller.rb | 3 ++- app/views/products/edit.html.erb | 5 +++-- config/routes.rb | 9 +-------- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb index b37d537..421d597 100644 --- a/app/controllers/orders_controller.rb +++ b/app/controllers/orders_controller.rb @@ -23,8 +23,9 @@ class OrdersController < ApplicationController if @order.save order_products = order_products_params order_products.each do |k, v| - @order.order_products.create(product: Product.find(k), count: v[:count]) if v[:count].to_i > 0 + @order.order_products.build(product: Product.find(k), count: v[:count]) if v[:count].to_i > 0 end + @order.save redirect_to root_path else @order_products = @order.order_products diff --git a/app/views/products/edit.html.erb b/app/views/products/edit.html.erb index ff64ced..b3a8bb7 100644 --- a/app/views/products/edit.html.erb +++ b/app/views/products/edit.html.erb @@ -3,6 +3,7 @@
<%= form_for(@product) do |f| %> + <%= render 'application/errors', model: @product %> <%= f.label :name %> <%= f.text_field :name %> @@ -13,8 +14,8 @@ <%= f.label :sale_price %> <%= f.number_field :sale_price %> - <%= f.label :img_path %> - <%= f.text_field :img_path %> + <%= f.label :avatar %> + <%= f.file_field :avatar %> <%= f.submit "Save changes", class: "btn btn-primary" %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index f4dac29..a049993 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,14 +8,7 @@ Rails.application.routes.draw do resources :products - get 'overview' => 'orders#overview' - - #products - get 'products' => 'products#new' - post 'products' => 'products#create' - - - + get 'overview' => 'orders#overview' # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes".