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])
|
||||
@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
|
||||
|
|
|
@ -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
|
||||
|
|
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,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| %>
|
||||
<div class="input-group">
|
||||
<span class="input-group-btn">
|
||||
<button class="btn btn-default btn-dec" type="button">
|
||||
<span class="glyphicon glyphicon-minus"></span>
|
||||
</button>
|
||||
</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>
|
||||
<% end %>
|
||||
<% end %>
|
||||
<%= f.fields_for :order_products do |op_field| %>
|
||||
<div class="input-group">
|
||||
<%= render partial: 'btn_dec' %>
|
||||
<%= op_field.text_field :count, class: 'form-control', value: 0 %>
|
||||
<%= render partial: 'btn_inc' %>
|
||||
<%= op_field.hidden_field :product_id %>
|
||||
</div>
|
||||
<% end %>
|
||||
<%= f.submit "Order", class: "btn btn-primary " %>
|
||||
<% end %>
|
||||
|
@ -29,6 +17,6 @@
|
|||
|
||||
<div style="padding-top: 50px;" class="order_list ">
|
||||
<p>
|
||||
Nothing ordered yet!
|
||||
Nothing ordered yet!
|
||||
</p>
|
||||
</div>
|
||||
|
|
Loading…
Reference in a new issue