From c3729a59646577f8fb6b0f88e25fda1937fe5afb Mon Sep 17 00:00:00 2001 From: Benjamin Cousaert Date: Fri, 5 Dec 2014 16:15:07 +0100 Subject: [PATCH] new order with decent rails --- app/controllers/orders_controller.rb | 12 ++--------- app/models/order.rb | 2 +- app/views/application/_btn_dec.html.erb | 5 +++++ app/views/application/_btn_inc.html.erb | 5 +++++ app/views/orders/_order.html.erb | 26 ----------------------- app/views/orders/_order_button.html.erb | 5 ----- app/views/orders/new.html.erb | 28 +++++++------------------ 7 files changed, 21 insertions(+), 62 deletions(-) create mode 100644 app/views/application/_btn_dec.html.erb create mode 100644 app/views/application/_btn_inc.html.erb delete mode 100644 app/views/orders/_order.html.erb delete mode 100644 app/views/orders/_order_button.html.erb diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb index d7f0fce..b474a74 100644 --- a/app/controllers/orders_controller.rb +++ b/app/controllers/orders_controller.rb @@ -20,14 +20,10 @@ class OrdersController < ApplicationController @user = User.find(params[:user_id]) @order = @user.orders.build(order_params) @products = Product.all - @products.each do |p| - @order.order_products.build(product: p, count: order_products_params[p.id.to_s][:count]) - end - + @order_products = @order.order_products if @order.save redirect_to root_path else - @order_products = @order.order_products render 'new' end end @@ -35,10 +31,6 @@ class OrdersController < ApplicationController private def order_params - params.require(:order).permit() - end - - def order_products_params - params.require(:order).permit({:products => [:product_id, :count]})[:products] + params.require(:order).permit(order_products_attributes: [:product_id, :count]) end end diff --git a/app/models/order.rb b/app/models/order.rb index b17cf58..cedcc3e 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -25,6 +25,6 @@ class Order < ActiveRecord::Base validates :user, presence: true - accepts_nested_attributes_for :order_products, reject_if: proc { |op| op.count <= 0 } + accepts_nested_attributes_for :order_products, reject_if: proc { |op| op[:count].to_i <= 0 } end diff --git a/app/views/application/_btn_dec.html.erb b/app/views/application/_btn_dec.html.erb new file mode 100644 index 0000000..dd3e42b --- /dev/null +++ b/app/views/application/_btn_dec.html.erb @@ -0,0 +1,5 @@ + + + diff --git a/app/views/application/_btn_inc.html.erb b/app/views/application/_btn_inc.html.erb new file mode 100644 index 0000000..d1246cb --- /dev/null +++ b/app/views/application/_btn_inc.html.erb @@ -0,0 +1,5 @@ + + + diff --git a/app/views/orders/_order.html.erb b/app/views/orders/_order.html.erb deleted file mode 100644 index 1740c06..0000000 --- a/app/views/orders/_order.html.erb +++ /dev/null @@ -1,26 +0,0 @@ -
  • - - - - - - - Ordered <%= time_ago_in_words(order.created_at) %> ago. - -
  • - - - diff --git a/app/views/orders/_order_button.html.erb b/app/views/orders/_order_button.html.erb deleted file mode 100644 index 7f7a5b4..0000000 --- a/app/views/orders/_order_button.html.erb +++ /dev/null @@ -1,5 +0,0 @@ - diff --git a/app/views/orders/new.html.erb b/app/views/orders/new.html.erb index 09a44c3..63be5c0 100644 --- a/app/views/orders/new.html.erb +++ b/app/views/orders/new.html.erb @@ -3,25 +3,13 @@ <%= form_for( @order, url: user_orders_path(@user)) do |f| %> <%= render 'application/errors', model: @order %> - <% @order_products.each do |op| %> - <%= f.fields_for :products do |products| %> - <%= products.fields_for op.product.id.to_s do |product_field| %> -
    - - - - <%= product_field.text_field :count, class: 'form-control', value: 0 %> - <%= image_tag op.product.avatar %> - - - -
    - <% end %> - <% end %> + <%= f.fields_for :order_products do |op_field| %> +
    + <%= render partial: 'btn_dec' %> + <%= op_field.text_field :count, class: 'form-control', value: 0 %> + <%= render partial: 'btn_inc' %> + <%= op_field.hidden_field :product_id %> +
    <% end %> <%= f.submit "Order", class: "btn btn-primary " %> <% end %> @@ -29,6 +17,6 @@

    - Nothing ordered yet! + Nothing ordered yet!