new order with decent rails
This commit is contained in:
parent
7ddd1d5775
commit
c3729a5964
7 changed files with 21 additions and 62 deletions
|
@ -20,14 +20,10 @@ class OrdersController < ApplicationController
|
||||||
@user = User.find(params[:user_id])
|
@user = User.find(params[:user_id])
|
||||||
@order = @user.orders.build(order_params)
|
@order = @user.orders.build(order_params)
|
||||||
@products = Product.all
|
@products = Product.all
|
||||||
@products.each do |p|
|
@order_products = @order.order_products
|
||||||
@order.order_products.build(product: p, count: order_products_params[p.id.to_s][:count])
|
|
||||||
end
|
|
||||||
|
|
||||||
if @order.save
|
if @order.save
|
||||||
redirect_to root_path
|
redirect_to root_path
|
||||||
else
|
else
|
||||||
@order_products = @order.order_products
|
|
||||||
render 'new'
|
render 'new'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -35,10 +31,6 @@ class OrdersController < ApplicationController
|
||||||
private
|
private
|
||||||
|
|
||||||
def order_params
|
def order_params
|
||||||
params.require(:order).permit()
|
params.require(:order).permit(order_products_attributes: [:product_id, :count])
|
||||||
end
|
|
||||||
|
|
||||||
def order_products_params
|
|
||||||
params.require(:order).permit({:products => [:product_id, :count]})[:products]
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -25,6 +25,6 @@ class Order < ActiveRecord::Base
|
||||||
|
|
||||||
validates :user, presence: true
|
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
|
end
|
||||||
|
|
5
app/views/application/_btn_dec.html.erb
Normal file
5
app/views/application/_btn_dec.html.erb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
<span class="input-group-btn">
|
||||||
|
<button class="btn btn-default btn-dec" type="button">
|
||||||
|
<span class="glyphicon glyphicon-minus"></span>
|
||||||
|
</button>
|
||||||
|
</span>
|
5
app/views/application/_btn_inc.html.erb
Normal file
5
app/views/application/_btn_inc.html.erb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
<span class="input-group-btn">
|
||||||
|
<button class="btn btn-default btn-inc" type="button">
|
||||||
|
<span class="glyphicon glyphicon-plus"></span>
|
||||||
|
</button>
|
||||||
|
</span>
|
|
@ -1,26 +0,0 @@
|
||||||
<li id="order-<%= order.id %>">
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<span class="products"></span>
|
|
||||||
<span class="timestamp">
|
|
||||||
Ordered <%= time_ago_in_words(order.created_at) %> ago.
|
|
||||||
</span>
|
|
||||||
</li>
|
|
||||||
|
|
||||||
|
|
||||||
<script charset="utf-8">
|
|
||||||
|
|
||||||
$(document).ready(function() {
|
|
||||||
//$(".products").empty()
|
|
||||||
var op = '<%=order.products%>'.replace(/"/g , "\"" );
|
|
||||||
console.log(op);
|
|
||||||
var products = JSON.parse(op);
|
|
||||||
console.log(products)
|
|
||||||
|
|
||||||
$.each(products, function(i, product) {
|
|
||||||
$(".products").append("<p> - "+ i +": "+ product.number_of +"</p>");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
</script>
|
|
|
@ -1,5 +0,0 @@
|
||||||
<button class="btn btn-default order-btn"
|
|
||||||
data-name= '<%= p.name %>'
|
|
||||||
date-sale-price= '<%= p.sale_price %>'>
|
|
||||||
<%= p.name %>
|
|
||||||
</button>
|
|
|
@ -3,26 +3,14 @@
|
||||||
<%= form_for( @order, url: user_orders_path(@user)) do |f| %>
|
<%= form_for( @order, url: user_orders_path(@user)) do |f| %>
|
||||||
<%= render 'application/errors', model: @order %>
|
<%= render 'application/errors', model: @order %>
|
||||||
|
|
||||||
<% @order_products.each do |op| %>
|
<%= f.fields_for :order_products do |op_field| %>
|
||||||
<%= f.fields_for :products do |products| %>
|
|
||||||
<%= products.fields_for op.product.id.to_s do |product_field| %>
|
|
||||||
<div class="input-group">
|
<div class="input-group">
|
||||||
<span class="input-group-btn">
|
<%= render partial: 'btn_dec' %>
|
||||||
<button class="btn btn-default btn-dec" type="button">
|
<%= op_field.text_field :count, class: 'form-control', value: 0 %>
|
||||||
<span class="glyphicon glyphicon-minus"></span>
|
<%= render partial: 'btn_inc' %>
|
||||||
</button>
|
<%= op_field.hidden_field :product_id %>
|
||||||
</span>
|
|
||||||
<%= product_field.text_field :count, class: 'form-control', value: 0 %>
|
|
||||||
<%= image_tag op.product.avatar %>
|
|
||||||
<span class="input-group-btn">
|
|
||||||
<button class="btn btn-default btn-inc" type="button">
|
|
||||||
<span class="glyphicon glyphicon-plus"></span>
|
|
||||||
</button>
|
|
||||||
</span>
|
|
||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
<% end %>
|
|
||||||
<% end %>
|
|
||||||
<%= f.submit "Order", class: "btn btn-primary " %>
|
<%= f.submit "Order", class: "btn btn-primary " %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue