Fix view of orders/new
This commit is contained in:
parent
cb17fdc1a7
commit
95aaca802f
6 changed files with 47 additions and 13 deletions
|
@ -1,3 +1,15 @@
|
|||
# Place all the behaviors and hooks related to the matching controller here.
|
||||
# All this logic will automatically be available in application.js.
|
||||
# You can use CoffeeScript in this file: http://coffeescript.org/
|
||||
ready = ->
|
||||
$('.btn-inc').on 'click', ->
|
||||
input = $(this).parent().parent().find('input').first()
|
||||
input.val(parseInt(input.val()) + 1)
|
||||
|
||||
$('.btn-dec').on 'click', ->
|
||||
input = $(this).parent().parent().find('input').first()
|
||||
if input.val() != '0'
|
||||
input.val(parseInt(input.val()) - 1)
|
||||
|
||||
$(document).ready(ready)
|
||||
$(document).on('page:load', ready)
|
||||
|
|
|
@ -8,26 +8,29 @@ class OrdersController < ApplicationController
|
|||
@user = User.find(params[:user_id])
|
||||
@order = @user.orders.build
|
||||
@products = Product.all
|
||||
@order_products = @order.order_products
|
||||
|
||||
@products.each do |p|
|
||||
@order.order_products.build(product: p)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def create
|
||||
@user = User.find(params[:user_id])
|
||||
@order = @user.orders.build(order_params)
|
||||
if @order.save
|
||||
#flash[:success] = "order created!"
|
||||
redirect_to overview_path
|
||||
else
|
||||
@products = Product.all
|
||||
render 'new'
|
||||
@order = @user.orders.build
|
||||
@products = Product.all
|
||||
@order_products = @order.order_products
|
||||
|
||||
@products.each do |p|
|
||||
@order.order_products.build(product: p)
|
||||
end
|
||||
render 'new'
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def order_params
|
||||
#params.require(:order).permit(:products)
|
||||
#
|
||||
params.require(:order).permit(:products)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -11,6 +11,8 @@ class Order < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
accepts_nested_attributes_for :order_products
|
||||
|
||||
default_scope -> { order('created_at DESC') }
|
||||
|
||||
end
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
class OrderProduct < ActiveRecord::Base
|
||||
belongs_to :order
|
||||
belongs_to :product
|
||||
|
||||
accepts_nested_attributes_for :product
|
||||
end
|
||||
|
|
|
@ -1,10 +1,25 @@
|
|||
<%= @user.name %>
|
||||
|
||||
<% @products.each do |p| %>
|
||||
<%= render "orders/order_button" , p: p %>
|
||||
<% end %>
|
||||
|
||||
<%= form_for( @order, url: user_orders_path(@user)) do |f| %>
|
||||
<% @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 %>
|
||||
<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 %>
|
||||
<% end %>
|
||||
<%= f.submit "Order", class: "btn btn-primary " %>
|
||||
<% end %>
|
||||
|
||||
|
|
Loading…
Reference in a new issue