Skip to content

Instantly share code, notes, and snippets.

/form.html.erb Secret

Created October 17, 2016 06:42
Show Gist options
  • Save anonymous/edbdc7a63129b55c832e8267a243d3e5 to your computer and use it in GitHub Desktop.
Save anonymous/edbdc7a63129b55c832e8267a243d3e5 to your computer and use it in GitHub Desktop.
<%= 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 %>
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