/form.html.erb Secret
Created
October 17, 2016 06:42
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= form_tag serialized_products_path do %> | |
<% @serialized_products.each do |product| %> | |
<%= fields_for 'serialized_products[]', product do |p| %> | |
<%= p.label :product_id %><br> | |
<%= p.text_field :product_id %> | |
<%= p.label :location_id %><br> | |
<%= p.text_field :location_id %> | |
<%= p.label :serial %><br> | |
<%= p.text_field :serial %> | |
<% end %> | |
<% end %> | |
<%= submit_tag %> | |
<% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SerializedProductsController < ApplicationController | |
before_action :set_serialized_product, only: [:show, :edit, :update, :destroy] | |
def new | |
if session[:reference] == nil or session[:location_id] == nil or session[:line_item_ids] == nil | |
print "\n\nSession is nil.\n\n" | |
redirect_back(fallback_location: root_path) | |
end | |
@reference = session[:reference] | |
location_id = session[:location_id] | |
@line_items = InventoryAdjustmentItem.where(id: session[:line_item_ids]) | |
@serialized_products = [] | |
@line_items.each do |line_item| | |
(line_item.quantity).times do | |
@serialized_products.push(SerializedProduct.new(product_id: line_item.product_id, location_id: location_id)) | |
end | |
end | |
end | |
def create | |
params["serialized_products"].each do |serialized_product| | |
if serialized_product["serial"].present? | |
SerializedProduct.create(serialized_product_params(serialized_product)) | |
end | |
end | |
redirect_to :root, notice: 'Serialized product was successfully created.' | |
session[:reference] = nil | |
session[:location_id] = nil | |
session[:line_item_ids] = nil | |
end | |
private | |
def set_serialized_product | |
@serialized_product = SerializedProduct.find(params[:id]) | |
end | |
def serialized_product_params(my_params) | |
my_params.permit(:product_id, :location_id, :serial) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment