Add stock form

This commit is contained in:
benji 2015-02-08 10:11:23 +01:00
parent ca3045fbbf
commit 39e96305b6
5 changed files with 35 additions and 3 deletions

View file

@ -39,10 +39,22 @@ class ProductsController < ApplicationController
redirect_to action: :index
end
def stock
@products = Product.all
end
def update_stock
@products = Product.all
@products.each do |product|
stock_inc = params[:products][product.id.to_s][:stock_inc].to_i
product.increment!(:stock, stock_inc) if stock_inc > 0
end
redirect_to products_path
end
private
def product_params
params.require(:product).permit(:name, :price, :avatar, :category, :stock)
end
end

View file

@ -32,6 +32,7 @@
<ul class="dropdown-menu" role="menu">
<li><%= link_to "List", products_path %></li>
<li><%= link_to "Add product" , new_product_path %></li>
<li><%= link_to "Add stock", stock_products_path %></li>
</ul>
</li>
<li class="dropdown">

View file

@ -2,7 +2,7 @@
<div class="thumbnail">
<%= image_tag product.avatar %>
<div class="caption">
<h3><%= product.name %> - <%= euro(product.price) %></h3>
<h3><%= product.name %> - <%= euro(product.price) %> (<%= product.stock %>)</h3>
<% if current_user.admin? %>
<p>
<%= link_to "Edit", edit_product_path(product), class: "btn btn-default" %>

View file

@ -0,0 +1,14 @@
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_tag do %>
<% @products.each do |product| %>
<%= fields_for "products[]", product do |form| %>
<%= image_tag product.avatar %>
<%= form.number_field :stock_inc, value: 0, class: 'form-control' %>
<% end %>
<% end %>
<%= submit_tag "Insert stock", class: 'btn btn-primary' %>
<% end %>
</div>
</div>

View file

@ -17,7 +17,12 @@ Rails.application.routes.draw do
get 'dagschotel/:product_id' => 'users#dagschotel', as: "dagschotel"
end
resources :products
resources :products do
collection do
get 'stock' => 'products#stock', as: 'stock'
post 'stock' => 'products#update_stock', as: 'update_stock'
end
end
get 'admins' => 'admins#schulden', as: "admins_schulden"
get 'overview' => 'orders#overview', as: "orders"
end